views:

324

answers:

16

I write/modify code in multiple file formats like perl, html, css, php, javascript, autohotkey script, ... etc.

I often search my personal library of source code for examples of syntax or complex logic for reuse in new code. Or, I will search through a directory tree for code references to a particular string (e.g. all references to a particular css style within perl, php, html, and javascript). Sometimes I even search for cryptic strings like =~, because I am searching for a particular regular expression in one of my perl programs.

Sometimes I search existing code using copernic, but unfortunately it can only search for words and automatically ignores any programming syntax. Windows 7 file search seems worse than Windows XP file search.

My question is... How do you search through your own libraries of source code?

+3  A: 

Use Google desktop to index and full text search your source files.

Kees de Kooter
+6  A: 

With Cygwin:

$ find /path/to/lib -name \*.pm | xargs grep -l foo

Be sure to quote funky operators so the shell leaves them alone, e.g.,

$ find . | xargs grep '=~'
Greg Bacon
A: 

When I have to do that kind of search (i.e. I want to find an exact portion of a code), I eiter use the "search into directory", or "search into directory by regex" fonctionnality of Eclipse PDT (the IDE I'm working with), or end up using grep on the command-line.

(For those who are on windows and/or don't like the CLI, there are some graphical tools that act like grep -- I've seen some colleagues use them)

Pascal MARTIN
+3  A: 

Download Cygwin and learn to use Grep which is included with it. You may be able to get a native version of grep but Cygwin has lots of useful stuff so I just install all available packages.

Jared
+1  A: 

I open up the project(s) I believe the desired code is in and use my editor's "Find in Project" option.

Ty W
+4  A: 

I usually use the search function in my IDE (nuSphere phpEd). It is reasonably fast, and allows me to filter by file types. Windows' search facility is useless, and somehow manages to get worse in every new version.

Anyway, I asked a question about programming-friendly search programs a while back. Maybe one of the answers helps.

Pekka
Thanks Pekka... that link is very helpful.
panofish
+1  A: 

OSX has something called SpotLight and a command interface called mdfind. Those are the best local search tools you can have if you are on a Mac.

fuzzy lollipop
+1 for Spotlight - I have 25+ years worth of code that I have written on my Mac which I can search instantaneously using Spotlight
Paul R
+13  A: 

I used to use the "find | xargs grep" trick till I found ack. Now I use it quite a bit.

larrys
I will check into this one.
panofish
It really is better than grep. The only problem for me is that I sometimes work with less common languages and it doesn't recognize their source files as files it should read. You can add more file types to the list of types it supports, though, so it's not too bad.
pib
A: 

Have a consistent naming scheme. Is it memset() or setmem()? InitializeUart() or UartInitialize() or UARTInitialize() or UARTInit()?

Robert
A: 

In a windows environment I use Examine32. It's a pretty handy generic string searcher (is that a valid phrase? :-) ).

SOA Nerd
A: 

For C code, I use the BSD coding style, which states that a function named foo should be defined thus:

char *
foo(int x)
{
    ...
}

With this style, the function definition is the only place in the source code where the function name may appear at the beginning of a line. Then it is only a matter to use grep:

grep '^foo' **/*.c

This trick filters out usages of the function, to concentrate on the definition.

When **/*.c yields too many file names (for really big source code trees), then find is to be used:

find src -name \*.c -exec grep '^foo' '{}' +

Note here the use of + with -exec: this supersedes use of xargs.

Thomas Pornin
A: 

TotalCommander's search functionality

Ozzie Perez
A: 

I generally know that I'm about to do something a bit finicky, so I whack a comment nearby the section. The comment should always have enough keywords in it for me to find what I'm after.

Then it's just a case of some arseabout find/grep combination.

Richo
A: 

Since I spend most of the day frantically typing random bits of code into Emacs in the vain hope that one of them will do something useful, I often use the emacs find-dired command:

  • Meta-x find-dired
  • Enter the path of the directory under which I think I'll find the interesting code
  • When it asks me for find arguments, enter (for example): -name '*.rb'
  • When it brings up the list of matching files, %m. to select them all (yay for Emacs's obvious key mapping)
  • Then meta-A and enter a search string.
  • Meta-, (there's another obvious key mapping for you) to go to the next match.

If this isn't a grand example of a $20 tool being used to solve a ten cent problem, I don't know what is.

Wayne Conrad
A: 

The Link provided by Pekka provided the best answer for me so far.

Powergrep is the solution that closest to what I was looking for. It's not perfect, but for a windows GUI solution... I like it.

panofish
A: 

See SD Source Code Search Engine. This breaks source code into fragments corresponding to the elements of each language (keywords, identifiers, strings, constants, numbers, operators). You can formulated searches restricted to just one type (of code) file, to a particular set of files, to identifiers matching foo across all files, setc.

The Search Engine pre-indexes the code to enable searches across thousands of files far faster than grep, It shows you hits in one GUI window, and the complete source code of a selected hit in another. You can launch your editor on the source code.

Ira Baxter