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:
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:
- 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.
- Set up a custom key binding for the "Find Text in Project" command
- Go to "Window" -> "Preferences..." -> "General" -> "Keys"
- Click on the "Modify" tab
- In the "Command": "Category:" dropdown, select "Search"
- In the "Command": "Name:" dropdown, select "Find Text in Project"
- In the "Key Sequence": "Name:" box, press CTRL+G
- Click the "Add" button
- Click "OK"
- In a source file, select some text and press CTRL+G
- In the search window, double-click a file or click the up or down arrows
Comments
You could also like to highlight your grep patterns on the current
edited file (Google Toolbar-like) with the following plugin:
SimpleSearch
Thnx... it helped me too.. facing same situation.
Your comment looks like Spam.
disqus:3286463269