SaltyCrane Blog — Notes on JavaScript and web development

How to do a grep-like search through multiple files in Eclipse/CDT

I read a lot of code written by other people and need to trace variables, function calls, include files, etc. so I've been doing a lot of grepping (e.g."grep variablename *") for variable and function names in my directory of c and h files. Then I'll open the file, find the search term and then grep again for a new search term. I actually wrote a code browser for a previous project in Perl (in the days before I converted to Python). It worked well, but it was not robust enough for a generic C project. I tried various tools including Red Hat's Source Navigator, Wind River's Sniff+, and Understand for C/C++. They were good tools, but they didn't do exactly what I wanted-- which was to trace through multiple source files quickly looking for a given search term. I was about to start rewriting my code browser tool using bison/flex or maybe PLY, when I decided to try Eclipse.

Suprisingly, Eclipse met my needs satisfactorily. Using the "Find Text in Project" command and a custom key binding, I may have a significantly minimized my use of the grep command. I am able to select a variable name in a source file, then press CTRL+G, and Eclipse gives a list of all files containing that variable. Double-clicking a file opens it with all the search terms highlighted in a tasteful purple color. Clicking the up and down arrows in the search window navigates me through all the occurrences in the files. I know this doesn't seem like a big deal, but it's the first time I've found this capability to work to my liking. (Admittedly, I have not tried all the tools. I'm sure Emacs users would be especially unimpressed.)

Here are the steps:
  1. Set up an Eclipse 3.2 / CDT3.1 project. If you don't need to do any building, you can use these instructions from my previous post.
  2. Set up a custom key binding for the "Find Text in Project" command
    1. Go to "Window" -> "Preferences..." -> "General" -> "Keys"
    2. Click on the "Modify" tab
    3. In the "Command": "Category:" dropdown, select "Search"
    4. In the "Command": "Name:" dropdown, select "Find Text in Project"
    5. In the "Key Sequence": "Name:" box, press CTRL+G
    6. Click the "Add" button
    7. Click "OK"
  3. In a source file, select some text and press CTRL+G
  4. In the search window, double-click a file or click the up or down arrows
Technorati tags: , ,

Comments


#1 Emilio Monti commented on :

You could also like to highlight your grep patterns on the current
edited file (Google Toolbar-like) with the following plugin:
SimpleSearch


#2 vikas commented on :

Thnx... it helped me too.. facing same situation.


#3 Miromico Zurich commented on :

Your comment looks like Spam.

disqus:3286463269