Getting colored results when using a pipe from grep to less
Solution 1
When you simply run grep --color
it implies grep --color=auto
which detects whether the output is a terminal and if so enables colors. However, when it detects a pipe it disables coloring. The following command:
grep --color=always -R "search string" * | less
Will always enable coloring and override the automatic detection, and you will get the color highlighting in less
.
EDIT: Although using just less
works for me, perhaps older version require the -R
flag to handle colors, as therefromhere suggested.
Solution 2
You can put this in your .bashrc
file:
export GREP_OPTIONS="--color=always"
or create an alias like this:
alias grepc="grep --color=always"
and you will need to use the -R
option for less
, as pointed out by therefromhere
Solution 3
In case like this, I prefer to actually create small sh files and put them on /usr/local/bin
.
I usually use grep
in the recursive way on the pwd
, so thats my personal script:
#!/bin/sh
grep --color=always -r "$@" . | less -R
And then I've just copied it as /usr/local/bin/g
(yes, I use it a lot)
Solution 4
Don't alias "grep", better to alias "less" which is never used by shells. In your .bashrc
just put: alias less="less -r"
.
Related videos on Youtube
Jeremy Powell
Updated on September 17, 2022Comments
-
Jeremy Powell over 1 year
I use the --colour option of grep a lot, but I often use less as well. How can I pipe grep results to less and still preserve the coloring. (Or is that possible?)
grep "search-string" -R * --colour | less
EDIT:
I'm looking for a direct solution or anything equivalent to this.
-
Ciro Santilli Путлер Капут 六四事 over 9 yearspossible duplicate of Get colors in 'less'' command
-
Shayan almost 5 yearsWhat does
*
do? From the man page of grep:*: The preceding item will be matched zero or more times.
But I still don't understand..! @JeremyPowell -
NeilG over 4 years@Shayan, the '*' in this case is for the file arguments. It gets processed by the shell which expands it to all files in the directory. The search string is enclosed in double quotes in the example.
-
-
pion over 14 yearsYou need to use
less -R
for the colour encoding to be interpreted by less correctly -
Drona over 14 yearsIt worked for me with just
less
, it may be version dependent. -
Jeremy Powell over 14 yearswow. I thought 'auto' only depended on the terminal type. I may be jumping the gun, but this may revolutionize the way I use linux :P
-
mctylr about 14 yearsWarning!: GREP_OPTIONS="--color=always" may break many scripts that use grep (or (e|f)grep).
-
asmeurer almost 13 yearsYeah, better to just alias grep. You can always get pure
grep
withGREP
, or override the--color
option manually. -
Owen Blacker about 12 yearsAwesome. Though I too to use
less -R
to getless
to display colours, rather than the colouring escape codes :o) -
Owen Blacker about 12 yearsA (hopefully) useful addendum: I needed to exclude some matches but maintain the colouring, so I actually ended up with
grep pattern file | grep -v badpattern | grep --colour=always pattern | less -R
, which met my needs perfectly. (Thanks again!) -
brandizzi about 11 yearsNot quite right. One needs to use both
grep --color=always
andless -R
. Note that grep only knows it is being piped into some other process and the--color=auto
option uses solely this information to decide if will output colors or not. -
David Winiecki over 10 yearsYay, works for git log -p too. :)
-
Craig McQueen over 10 yearsNote that
less
option-r
is different than-R
. Probably-R
is safer. -
not2qubit over 10 yearsSo why down-vote my solution. The OP specifically ask for
less
with the example already using `--color' option. -
saeedgnu over 10 yearsThis doesn't work for me, alias does work though.
-
Ray Foss over 9 yearsUsing grep without
-R
andmore
instead ofless -R
works for me... and its shorter. In other words, this works on Fedora 20grep --color=always -Irn "foo_bar" | more
-
cfi over 8 years@spatz: You could integrate Dennis answer into yours to make it a one-stop-shop. Integration answers are perfectly wanted on SO. Thanks! That would solve the problem Owen addressed in his comment.
-
greyfade almost 8 yearsAn alias is probably undesirable here.
less
supports a$LESS
environment variable. So, instead of an alias,export LESS='-R'
might be preferable. -
greyfade almost 8 years@OwenBlacker It might not be an alias. You might have
$LESS
set with-R
. -
00dani over 7 yearsWhy not just use shell functions for this kind of thing?
g() { grep --color=always -r "$@" . | less -R }
works identically and probably will give (minutely) better performance. -
Ligemer about 6 yearsUbuntu 16.04
cat file.log | grep --color=always password | less -R
-
Rory O'Kane about 6 yearsTo make
less
default to always pass through colors, edit yourLESS
environment variable to include-R
or its long form--RAW-CONTROL-CHARS
. TheLESS
variable contains a space-separated list of default flags forless
. -
Iazel over 5 years@00dani yeah, that's a valid alternative too and sometimes I use it. Please note that in this case, most time is spent in IO and therefore there is no perceivable performance boost :) Another difference is that once in your PATH, this script can be used with other shell scripts and aliases too; the function instead needs to be explicitly loaded
-
Shayan almost 5 yearsWhat does
*
do? From the man page of grep:*: The preceding item will be matched zero or more times.
But I still don't understand..! -
Pepedou about 4 years@Shayan It will search for the text "search string" in all files.
-
Sujay Phadke over 3 yearsold Q and answer, but thank you! works like a charm.