panofish
panofish

Reputation: 7879

How do you search through your own libraries of source code?

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?

Upvotes: 19

Views: 1778

Answers (16)

Larry Shatzer
Larry Shatzer

Reputation: 3627

I used to use the find | xargs grep trick until I found ack. Now I use that quite a bit.

Upvotes: 13

Ira Baxter
Ira Baxter

Reputation: 95306

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.

Upvotes: 0

panofish
panofish

Reputation: 7879

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.

Upvotes: 0

Thomas Pornin
Thomas Pornin

Reputation: 74382

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.

Upvotes: 0

Wayne Conrad
Wayne Conrad

Reputation: 107959

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.

Upvotes: 0

richo
richo

Reputation: 8989

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.

Upvotes: 0

Ozzie Perez
Ozzie Perez

Reputation: 583

TotalCommander's search functionality

Upvotes: 0

SOA Nerd
SOA Nerd

Reputation: 943

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

Upvotes: 0

Robert Deml
Robert Deml

Reputation: 12532

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

Upvotes: 0

Greg Bacon
Greg Bacon

Reputation: 139411

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 '=~'

Upvotes: 6

user177800
user177800

Reputation:

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.

Upvotes: 2

Pekka
Pekka

Reputation: 449385

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.

Upvotes: 5

Ty W
Ty W

Reputation: 6814

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

Upvotes: 1

Jared
Jared

Reputation: 39877

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.

Upvotes: 3

Pascal MARTIN
Pascal MARTIN

Reputation: 400912

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)

Upvotes: 0

Kees de Kooter
Kees de Kooter

Reputation: 7195

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

Upvotes: 3

Related Questions