Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

When I have dircolors defined life is full of... color.

When I pipe ls through less to scroll around I lose the colors.

Any suggestions?

share|improve this question

2 Answers 2

up vote 48 down vote accepted

Most likely your ls is aliased to ls --color=auto, which tells ls to only use colors when its output is a tty. If you do ls --color (which is morally equivalent to ls --color=always), that will force it to turn on colors.

You could also change your alias to do that, but I wouldn't really call that a good idea. Better to make a different alias with --color.

less needs -R, too.

share|improve this answer
    
Why would you not call it a good idea to alias ls to ls --color or ls --color=always, for that matter? –  j08lue Apr 8 at 13:38

Try less with the -R option like this:

command | less -R

This works for me in a one-liner like this:

ls -la | grep --color=always bash | less -r

And like this too:

ls --color | less -R

But you have to tweak the primary output (the output of ls in this case) a bit with the --color parameter.

share|improve this answer
    
Thanks, this also works for ack-grep. ack-grep my_string --color | less -R –  igniteflow Jan 30 '14 at 14:25
1  
--color is not recognized: ls: illegal option -- - Running bash on osx. –  Scott Biggs Oct 17 '14 at 17:04
1  
@Scott Biggs: in OSX there is no --color; try ls -G –  Lu-Chi Jan 3 at 6:34
    
@Lu-Chi, Even with -G, the colors are missing –  Iulian Onofrei Feb 6 at 10:26
    
In OS X you need to set the environment variable CLICOLOR_FORCE (to anything) when the output of ls isn't directed to a terminal. This works for me: CLICOLOR_FORCE= ls -G | less -R –  jooon Feb 11 at 15:15

protected by HopelessN00b Jan 5 at 7:59

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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