I have seen on some Linux, that
grep is configured to highlight the match, and print the matching file.
How can I configure
grep to highlight matches?
alias cgrep='grep -nR --color'
$ cgrep somestring /path/to/dir/or/file/with*/possible/*wild.card
Also one of my favorites:
$ pgrep some-hanging-process
will list all pids of processes that match the name of some-hanging-process which you can use in following situation:
$ kill $(pgrep some-hanging-process)
I've found that the best way to pimp grep is to use ack, which is essentially recursive grep with an intelligent ignore list (e.g., doesn't search .svn directories, ignores backup files, etc.), colour highlighting of results and perl regexps. It's what you want grep to do 98.6% of the time.
I set this in my .bashrc , instead of redefining grep using an alias:
For me, this works on Linux, MacOSX & FreeBSD.
I'd recommend avoiding the environment variable GREP_OPTIONS, it will affect every invocation of grep, even those embedded within other tools. If those tools expect grep to behave one way, and you change that behaviour, it will and does break those tools.
Instead, you can create an alias, which works well. This will only affect calls to grep from your interactive shell (i.e. that you type yourself).
The final option, that I like most of all, is to create a wrapper script that invokes grep. I prefer this over an alias, because I can invoke this wrapper from other programs. E.g. in vim by setting vimgrep, so that my searches from within vim behave identically to searches at the command-line.
$ cat `which grp` #!/usr/bin/env bash grep -rI --color --exclude-dir=\.bzr --exclude-dir=\.git --exclude-dir=\.hg --exclude-dir=\.svn --exclude-dir=build --exclude-dir=dist --exclude=tags $*
Invoke this using:
$ grp pattern dir
$ grp pattern .
will search for instances of 'pattern' in all text files in the current dir and subdirs.
Notice that I call my script 'grp', rather than shadowing 'grep', so that I'm always aware of whether I'm invoking grep with my customised defaults or not.
By default I include:
-r : search subdirs recursive -I : skip binary files --color : highlight matches in color --exclude-dir : skip specified directories and their subdirs --exclude : skip specified files
I think everyone would want to skip source control dirs: .hg .git .bzr .svn
Skipping 'build' and 'dist' are Python-isms, and probably don't apply to most people. No doubt you'll develop your own idiosyncrasies as you work.
'tags' is the output of ctags, that I use for 'go to function definition' and the like in tools like vim. As such it contains at least one copy of every word and symbol from my source code, so it's worth skipping from your search results.
"$*" at the end is the bash syntax for "and all the other params from the command-line", so you can pass in the pattern and dir to search as normal, and override any other command line flags you'd like.
--color option has been already mentioned several times, but I'd like to add that it's possible to configure the color in which the matches will be highlighted using an environment variable
The color should be encoded using ANSI color codes, for reference
Black 0;30 Dark Gray 1;30 Blue 0;34 Light Blue 1;34 Green 0;32 Light Green 1;32 Cyan 0;36 Light Cyan 1;36 Red 0;31 Light Red 1;31 Purple 0;35 Light Purple 1;35 Brown 0;33 Yellow 1;33 Light Gray 0;37 White 1;37
Since I didn't see any examples of actually setting colors, here is a simple setup for GNU grep:
# turn on colors, natch. export GREP_OPTIONS="--color=auto" if [[ $(echotc Co) -ge 256 ]]; then # 256 color terminals export GREP_COLORS="mt=38;5;118:sl=:cx=:fn=38;5;18:ln=1;30:bn=37:se=30" else # everybody else export GREP_COLORS="mt=31:sl=:cx=:fn=34:ln=1;30:bn=30:se=30" fi
Be careful about
GREP_OPTIONS; don't use it for anything other than things like
--color=auto or it'll mess up any scripts that use grep on your system.