Can I get less not to monochrome its output?
E.g., the output from
git diff is colored, but
git diff | less is not.
git diff --color=always | less -r
--color=always is there to tell
git to output color codes even if the output is a pipe (not a tty). And
-r is there to tell
less to interpret those color codes and other escape sequences. Use
-R for ANSI color codes only.
Another option would be to enable colors and use 'less -r' as your pager.
git config --global color.ui true git config --global core.pager 'less -r'
This results in
[color] ui = true [core] pager = less -r
in your ~/.gitconfig
For more information see the Pro Git book.
--raw-control-chars) option to less, or also
-R (only ANSI escape sequences).
I have an alias for this in
alias rless='less -r'
tree has an option to force colors on:
tree -C | less -r
And so on for
ls -lR --color | less -r
Just to add another version on the "use less -r":
use the environment variable LESS with the value r (or add r to whatever it already is)
eg, as I use it in my .bashrc
(The X stops the screen from clearing when exiting less)
In case anyone is interested in paging a json with
less it can be achieved using:
jq -C <jq args> file.json | less -R
jq -C . file.json | less -R
I know this is old and many have already provided the right answer but I would like to add that it is always better to use
less -R and not
less -r if you only need ANSI colors as
-rmay case problems in displaying the characters.
From the manual:
-r or --raw-control-chars Causes "raw" control characters to be displayed. The default is to display control characters using the caret notation; for example, a control-A (octal 001) is displayed as "^A". Warn‐ ing: when the -r option is used, less cannot keep track of the actual appearance of the screen (since this depends on how the screen responds to each type of control character). Thus, var‐ ious display problems may result, such as long lines being split in the wrong place. -R or --RAW-CONTROL-CHARS Like -r, but only ANSI "color" escape sequences are output in "raw" form. Unlike -r, the screen appearance is maintained correctly in most cases. ANSI "color" escape sequences are sequences of the form: ESC [ ... m