Can less retain colored output?

Use:

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.

Possible values for color.ui can be found in the man page of git-config. The output of man git-config | grep "color.ui$" -A8 is

color.ui
    This variable determines the default value for variables such as color.diff and
    color.grep that control the use of color per command family. Its scope will expand as
    more commands learn configuration to set a default for the --color option. Set it to
    false or never if you prefer Git commands not to use color unless enabled explicitly
    with some other configuration or the --color option. Set it to always if you want all
    output not intended for machine consumption to use color, to true or auto (this is the
    default since Git 1.8.4) if you want such output to use color when written to the
    terminal.

Use -r (--raw-control-chars) option to less, or also -R (only ANSI escape sequences).

I have an alias for this in ~/.bashrc

alias rless='less -r'

Tags:

Colors

Less