Creates a customized search command

alias cr='find . 2>/dev/null -regex '\''.*\.\(c\|cpp\|pc\|h\|hpp\|cc\)$'\'' | xargs grep --color=always -ni -C2'
Creates a command alias ('cr' in the above example) that searches the contents of files matching a set of file extensions (C & C++ source-code in the above example) recursively within the current directory. Search configured to be in colour, ignore-case, show line numbers and show 4 lines of context. Put in shell initialisation file of your choice. Trivially easy to use, e.g: cr sha1_init
Sample Output
./openssl/signatures.c-600-xmlSecOpenSSLDsaSha1EvpInit(EVP_MD_CTX *ctx)
./openssl/signatures.c:602:    return SHA1_Init(ctx->md_data);
./openssl/signatures.c-706-    NULL,
./openssl/signatures.c-707-#else /* XMLSEC_OPENSSL_096 */
./openssl/signatures.c:708:    SHA1_Init,
./openssl/signatures.c-709-    SHA1_Update,
./openssl/signatures.c-710-    SHA1_Final,

By: chrisdrew
2009-01-26 08:54:25

These Might Interest You

  • Once issuing the command, hit "esc" and then "k" (not together) to enter the search mode at the shell prompt (each time), and invoke the search with "/" as if you would in vi. Type a command and see the most recently used instance of that command. Use "n" and "N" to go forward and backwards through other instances of that command.

    set -o vi
    andykazmaier · 2009-12-22 20:40:10 3
  • Very handy and time-saving. Do a 'ctrl+ r' on command prompt. You will see a "(reverse-i-search)`':" mark. Just type any sub-string of the command you want to search(provided you have used it sometime in the same session). Keep on searching by repeatedly pressing ctrl+r. Press enter once you get the desired command string. Show Sample Output

    ctrl + r
    Bluehive · 2009-06-25 06:51:38 4
  • Searches backwards through your command-history for the typed text. Repeatedly hitting Ctrl-R will search progressively further. Return invokes the command. Show Sample Output

    Ctrl-R <search-text>
    tarkasteve · 2009-09-20 05:07:31 1
  • Although less behaves more or less like vim in certain aspects, the vim regex for word boundaries (\< and \>) do not work in less. Instead, use \b to denote a word boundary. Therefore, if you want to search for, say, the word "exit", but do not want to search for exiting, exits, etc., then surround "exit" with \b. This is useful if you need to search for specific occurrences of a keyword or command. \b can also be used at just the beginning and end, if needed.

    kFiddle · 2009-04-11 22:05:12 1

What Others Think

I appreciate the use of the --color switch to grep.
root · 490 weeks and 3 days ago
May want to use the -s switch to grep, or fix it so it handles paths with spaces. i.e.: alias cr='find . 2>/dev/null -regex '\''.*\.\(c\|cpp\|pc\|h\|hpp\|cc\)$'\'' | xargs grep --color=always -ni -C2' Someone with more talent could probably make this work with weird filenames rather than just cutting them out
kaedenn · 486 weeks and 4 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: