I have ls and grep aliased to 'ls --color=auto' and 'grep --color=auto' for colorized output, but when I pipe to more the color is lost.
Neither more nor less seems to have a param for colorizing their output. Is there any way to do this?
Redirecting doesn't strip colors, but many commands will detect when they are sending output to a terminal, and will not produce colors by default if not.
If you just want to tell less to interpret color codes, use less -R .
The —-color parameter tells grep to color the search terms in the output, which helps them stand out when among all the other text on the line. You choose which color you want using the GREP_COLOR environment variable: export GREP_COLOR=36 gives you cyan, and export GREP_COLOR=32 gives you lime green.
The problem isn't that more
and less
aren't colourizing their output, it's that ls
is not outputting the colour because it's connected to another process rather than the terminal.
You can't easily get ls
to be any smarter about when it outputs colour, but you can add --color
to force it to output colour when you're piping it to more
When you have colour output, use ... |less -R
to make less
pass the colours through to the terminal instead of showing the escape codes as text
ls --color | less -r
Tested on Linux, GNU userland.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With