Frequently when I’m writing code I want to quickly find all references to a particular function/method/class/variable within the entire code base. Many IDEs provide this feature, but most of those programs are too bulky for me, I typically program in Vim or Gedit. So I wrote this bash script which greps all the files in the current directory for a specific term and pretty prints the output with filename and line numbers. It lets you limit your search to files with a specific extension with the -e option, or a different directory with the -d option. Anyway, this is all very simple, what I want to talk about is running this straight from Gedit with a single key press and getting sexy output, like this:
Running tool: GrepAll ./Connecter.py: 352: self.encoder.add_neighbor(self.id, AESKey(key, iv)) 356: self.encoder.add_neighbor(self.id, self.tmp_aes) ./Encoder.py: 154: def add_neighbor(self, nid, aeskey):
Right there in the bottom pane.
So along with Gedit’s many nice features like tabs, syntax highlighting, and remote file editing come a bunch of very useful plugins. One of which is called External Tools, and it allows you to setup key bindings for scripts.
To use Grepall in Gedit, go to Edit -> Preferences -> Plugins and check External Tools. A “Tools” menu should appear in your menu bar. Then go to Tools -> External Tools, create a new tool and specify a description and key-binding of your choice. Copy and paste the following into the “Commands” text box:
#!/bin/bash search=`xargs -0 echo` $HOME/bin/grepall -e `echo $GEDIT_CURRENT_DOCUMENT_NAME | cut -d. -f2` "$search"
(Make sure you either saved grepall as ~/bin/grepall or changed the part that says “$HOME/bin/grepall” in the above code. And don’t forget to chmod it executable)
Back on the External Tools dialog, for Input select “Current selection” and for Output select “Display in bottom pane”
That’s it! Now whenever you highlight some text and hit the key-binding you selected Gedit will show you all instances of the highlighted text in all the files of the current directory with the same extension as the file you’re editing.
-John








