When you pipe something through less, the program usually discards color codes because it's not outputting to a TTY. So you have to add --color=always to the options for it to work.

ls -l --color=always | less
grep -R asdf --color=always | less

What's worse is that sometimes things don't even support that option, so there's actually no way to force color output to a pipe.

Is there a (relatively) easy way to make less emulate a TTY so that I don't have to specify --color=always to every program when things get piped to it, and it automatically displays color output when possible?

link|improve this question
I don't ever recall seeing a TTY that printed in other than black. – DanH Feb 3 at 23:10
feedback

3 Answers

up vote 5 down vote accepted

It's not less that needs to change. The output of your other programs is being redirected to a pipe. Those programs detect that their output is not being sent to a tty and they disable their coloring. You're stuck with having to do something special with the source programs to color their output even when redirected to a pipe.

I think I have a solution for the programs that do not support a --color=always option. The unbuffer command creates a pty and sends the output of its argument program to that pty, therefore the argument program thinks its output is going to a tty and colors it.

I tried the following as an experiment and it worked. I couldn't think of any programs that color their output by default.

$ unbuffer ls --color=auto | cat

Also, don't you have to use the -r option with less to get it to display color? I also tried this:

$ unbuffer ls --color=auto | less -r
link|improve this answer
feedback

There isn't any way to make less allocate a pty, though, so programs that depend on a TTY for deciding to output color or not need to be manually told. Sorry.

link|improve this answer
feedback

on FreeBSD you can:

env CLICOLOR_FORCE=yes ls -l | less -R

or you can set/override options (csh/tcsh):

alias less less -R
setenv CLICOLOR_FORCE yes
setenv  LS_COLORS "no=00:fi=00:di=01;36:ln=01;37;44:pi=40;33:so=01;35"
setenv  LS_COLORS "$LS_COLORS":"bd=40;33;01:cd=40;33;01:or=40;31;01"
setenv  LS_COLORS "$LS_COLORS":"ex=01;32;40:*.tar=01;31:*.tgz=01;31"
setenv  LS_COLORS "$LS_COLORS":"*.arj=01;31:*.taz=01;31:*.lzh=01;31"
setenv  LS_COLORS "$LS_COLORS":"*.zip=01;31:*.z=01;31:*.Z=01;31"
setenv  LS_COLORS "$LS_COLORS":"*.gz=01;31:*.deb=01;31:*.jpg=01;35"
setenv  LS_COLORS "$LS_COLORS":"*.gif=01;35:*.bmp=01;35:*.ppm=01;35"
setenv  LS_COLORS "$LS_COLORS":"*.tga=01;35:*.xbm=01;35:*.xpm=01;35"
setenv  LS_COLORS "$LS_COLORS":"*.tif=01;35:*.mpg=01;37:*.avi=01;37"
setenv  LS_COLORS "$LS_COLORS":"*.gl=01;37:*.dl=01;37:*.tex=01;35"
setenv  LS_COLORS "$LS_COLORS":"*.ps=01;35"
setenv  LS_COLORS "$LS_COLORS":"*.php=01;33"
setenv  LS_COLORS "$LS_COLORS":"*.sh=00;31"
setenv  LS_COLORS "$LS_COLORS":"*.txt=01;37:*.conf=01;37:*.config=01;37"
setenv  LSCOLORS GxExcxdxCxegedabagacad

in ~/.cshrc

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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