I live inside my code editor all day and something that I need to do pretty often is to search for the definition of a symbol (a variable, constant, or function). For example, I might be debugging a problem in a JavaScript file when I come across this code:
//...
let newData = translateDataFromRaw(rawData);
//...
I need to see translateDataFromRaw
to understand how it works. How can I do this?
If I’m using an IDE or a project covered by a tags file, I can use the magical “jump to definition” feature. This is usually the best option as it is fast and nearly always accurate. However, if I’m using a simple code editor that does not have a “jump to definition” feature, or if the feature is still indexing my project (this can take quite a while sometimes), or if I want to find that function on the command-line for some other purpose without having to open up an IDE, then this will not work. What are my other options?
I could use grep
to search my files for the string translateDataFromRaw
and then find the definition from among the results. This works well and is pretty simple, unless that function is used in a lot of places. If there are hundreds of results, it may take me forever to find the one I want.
To help with this situation, I wrote a little command-line tool called grepdef. With it, I could just run grepdef translateDataFromRaw
and I’ll instantly get the grep result for the definition.
$ grepdef translateDataFromRaw
./src/translators.js:function translateDataFromRaw(data) {
It’s way faster than “jump to definition” on some of the projects I work with. I even have it integrated into my editors with the plugins vim-grepdef and vscode-grepdef.
How does it work?
It’s basically the same as the grep
technique I described above, except that it has two advantages:
First, it uses a regular expression to find symbol definitions unique to a specific code language. This can definitely be inaccurate, but in my experience it’s close enough.
Second, it uses ripgrep which is blazingly fast.
All together, grepdef
saves me hours of development time. Maybe it will help you too?
Right now it only supports two language types: PHP and JavaScript/TypeScript, but it’s built to be easily extensible to other languages. Please suggest them as issues!
Top comments (0)