How to search source code in grep

grep (command line)

Venerable grep works on pretty much every operating system around and is included by default on almost all Unix ones. It is not specificly designed for searching source code as it was designed to be a general purpose search tool but can fulfil that role very well. Its ubiquity is its great strength as knowing how to use it is portable across pretty much any system you will end up working on. The biggest issues with using grep to search source code is its lack of syntax highlighting, recursive searches include things you probably don't want to search across, and you need a few command line arguments to achieve your goal. For example,

grep -inIEr -C1 –include="*.java" "codere*" ./src/

Will search ignoring case (-i), printing line numbers (-n), ignoring binary files (-I), using posix regex (-E), recursively (-r), showing the context (-C1), only searching java files (-include) for "codre*" in the directory ./src/ This can be a bit of a handful to type out for every search. Another problem with grep is that it because it looks at every file and depending on your query every byte in every file it can take a very long time to search across large or multiple code bases.

You should only be using grep if you are unable to use any other code search solution. It is recommended to use ack in all situations where you would normally use grep.

Productivity through code search.

Try searchcode server for Free