Viewing colored output with less

When an output is filtered through less, the colored characters are transformed to black and white (or whatever your terminal settings are). This is because less, by default, strips away (filters out) escape sequences. These are special sequences which tell the terminal to do something special (like move the cursor, change the color and so on).

They are stripped away by default, so that they don’t interfere with the positioning of the text by less (for example if less counted that it outputted 24 lines, but on the 12th line there was an escape sequence telling the terminal to move to the top of the scree, the output would get mixed up).

If you want to let control characters through, you can use the -r switch. It is however safer to use the -R switch, which only lets control characters through which change the foreground/background color. Even so, your screen might get mixed up, so remember that q exits less and Ctrl+L clears your terminal. If the terminal is very badly messed up, try issuing the tset command.


Leave a Reply

Your email address will not be published. Required fields are marked *