Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

When I run a program (for example grep or ls) without a pager, its output is colored. However when I run it piping its output to less, no colors are shown.

For example, this command outputs colored output:

grep -r something

but this doesn't:

grep -r something | less

Why? How can I see colors through less?

share|improve this question
1  
grep --color=always "search string" * | less -R but I tend to use most nowadays instead of less. ALSO: gnu.org/software/src-highlite is a color highlighter (less works too) –  Rinzwind Jun 13 at 14:08
    
@alex have you installed python-pygments? –  souravc Jun 13 at 14:09
    
@souravc no I did not install that –  lion Jun 13 at 14:11
    
@ales ignoring me are we? >:-D –  Rinzwind Jun 13 at 14:21
    
@Rinzwind no no thanks –  lion Jun 13 at 16:00

2 Answers 2

up vote 8 down vote accepted

There are two problems here:

  • Commands like ls —which auto-detect the colour support— don't find support from pipes
  • less is set to just display colour codes by default.

Both can be overcome but it's a bit clunky:

ls --color=always | less -R

This isn't ls specific. Many commands that support colour also have an override argument.

share|improve this answer
1  
My last upvote (reached the limit) for today to support you for 100k. But me and Rinzwind want cake when that happens. –  hakermania Jun 13 at 14:53
    
I downloaded a program with name was ack-grep. when type ack-grep foo , it will search for foo on the all sub directories.So I did ack-grep foo | less, and the colors gone. But seems this program does not support "--color=always", whatever thanks for help –  lion Jun 13 at 15:02
1  
@alex ack has a --color argument: ack-grep -i select --color | less -R –  Oli Jun 13 at 15:07
    
@Oli .........Oh my God........ the only thing that I can say about your knowledge and help and also power of linux –  lion Jun 13 at 15:53
    
@ales he stole that line from my comment >:-D –  Rinzwind Jun 13 at 19:14

If you're interested in colors in less more generally, you might want to look at lesspipe.sh. See, for example, http://freecode.com/projects/lesspipe.

lesspipe.sh is an input filter for the pager less as described in less's man page. The script runs under a ksh-compatible shell (e.g. bash, zsh) and allows you to use less to view files with binary content, compressed files, archives, and files contained in archives.

It will also colorize shell scripts, perl scripts, etc. similarly to a text editor, but without the use of any "preprocessing" program to do the colorizing.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.