Pages

26 June, 2011

Using gedit's External Tools Plugin to Search in Files

IDEs for strongly typed languages allow you to press a hotkey (e.g., F3 in Eclipse) to look up a type or use features like "where used" to find all references of class, property, or method.

When hacking Javascript in gedit I also very often need to find all references of variables, object properties, and functions. But when coding with a weakly typed language like Javascript in gedit you do not have any shortcut to success. At least not by default.

Searching manually is not an option as soon as your projects grow larger. Firing up the gnome-search-tool is also not an option, since it would require further clicking in the dialog and cannot open gedit at the correct line.

Super Simple Search Script
Currently, the best option for me is to use this little script:
#!/bin/sh
args=$(xargs)
echo "searching for '$args'"
grep -nr $args * | sed 's/\(^[^\:]*:[0-9]*:\)/\1 /g' 
It simply looks up the current word in all files of the current document's directory and outputs "filename:line-number: matching line's text" in gedit's bottom pane. Output text using the "file:line:text" pattern is clickable in gedit and lets you jump to the line in the file immediately.


Installation
To use this script in gedit you need to enable the External Tools Plugin. Add a new entry in the plugin's configuration screen, name it, assign your hotkey, and select "Current word" as "Input". Finally copy the script code into the text area and check if everything is correct. Look at the first screenshot to check if everything is set up correctly.

Now you can finally test your new tool. Open a file and select a word. You can also set the cursor before any of the characters of the word. Now press your hotkey (F3 in my case) and the script should check your current file's directory for other files containing the word. Look at the second screenshot to see a sample result.

I hope you liked this article and that the script is useful for you.
Best wishes, Juve


2 comments:

Rails Developer said...

Above code works for simple strings like abcd but fails for abcd_efg.
If I search for abcd_efg it shows as
Searching for 'abcd in open documents

Rails Developer said...

It ignores special characters and string next to special character while reading.