Friday 1 August 2003 — This is almost 20 years old. Be careful.
I use Microsoft Visual Studio .NET for C++ work at work, and it is very powerful, and does lots of cool things. (Although, there are two things it doesn’t do as well as previous versions did: the class browser won’t show derived classes, and two documents of the same type can’t have different tab settings). One thing I wish it did better was finding strings in files. It finds them just fine, but there’s no way for me to keep the results: the next find overwrites the last results. I wanted to change that.
Of course, they provide a “Find Results 2” window, but please, if you are going to provide more than one of something, provide n, not 2. Everyone knows that the only natural numbers in computer science are zero, one, and infinity.
So I set out to provide a new way to find files. I spent about two seconds looking at the add-in architecture, which seems very complete, very powerful, and very daunting. I’d have to take a low-tech approach.
Here’s what I did:
- I wrote a Python script to read the .vcproj files (they’re XML) and dump a list of all the files in the project. projectfiles.py finds all the .vcproj files in the current directory or any of its subdirectories, and writes full file paths to stdout. You can give it file extensions on the command line, and it will limit its output to those files.
- Using projectfiles.py, I creates a projectfiles.txt file in my solution’s root directory.
- I grabbed a copy of Vivian De Smedt’s grep.py, renamed it pygrep.py (I have Cygwin’s grep on the path), and modified it to take an argument like “@projectfiles.txt” to search the files mentioned in the file. It will also label matches with a properly-formatted file and line number for output results in Visual Studio. Here’s my pygrep.py.
- I wrote a teeny batch file, projgrep.cmd:
- Finally, I configured an external tool to call projgrep.cmd with the currently selected text as an argument, to prompt for arguments, and to write the results to the Output window.
echo ---- %* ---------------------------------------- >> projgrep.txt
pygrep.py %* @projectfiles.txt >> projgrep.txt
The net result is I can find strings across all the files in my solution, the results get written to an ever-growing list of find results, those results are written into the Output window where they can be double-clicked to open the line, and F8 (goto next result) even works properly! It’s clunky, and the output window doesn’t work as smoothly (no results get written until the grep is done, and it has to write all the old results in first, which can take a while if I’ve been using the thing too long), but it does work.
I could improve it by combining the .cmd file and pygrep, but I’m tired of hacking it; I’ve got work to do.
Long live low tech!
grep 'myval' -r -C2 -n .
Add a comment: