简体   繁体   中英

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 (eg 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?

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

With Cygwin:

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

Be sure to quote funky operators so the shell leaves them alone, eg ,

$ find . | xargs grep '=~'

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.

使用Google桌面对源文件进行索引和全文搜索。

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.

我打开项目,我相信所需的代码在,并使用我的编辑器的“在项目中查找”选项。

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.

When I have to do that kind of search (ie 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)

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

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

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 .

TotalCommander的搜索功能

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM