583

Can I get less not to monochrome its output?

E.g., the output from git diff is colored, but git diff | less is not.

ripper234
  • 31,763

11 Answers11

693

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.

  • Excellent! Can I translate this to a "git config --global" command? – ripper234 Aug 24 '11 at 12:23
  • 5
    @ripper234. With recent gits, git config color.ui true should be enough to obtain colored output, and to automatically run a pager for long outputs. – Stéphane Gimenez Aug 24 '11 at 12:48
  • True enough, I was actually using something similar but not quite like it. Thanks anyway for the great answer. – ripper234 Aug 24 '11 at 13:20
  • @ripper234 you can set an alias for this: http://unix.stackexchange.com/a/50007/24272 – AvL Oct 05 '12 at 08:23
  • 6
    In parallel to this Q/A, watch --color 'git diff --cached --color=always' and its friends can bring you some additional awesomeness. – Alois Mahdal Jul 26 '13 at 09:19
  • 48
    Isn't using less -R better (or export LESS=R in /etc/profile)? Why would you allow it to display anything but ANSI "color" escape sequences? Also, the man page says Warning: 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, various display problems may result, such as long lines being split in the wrong place. – x-yuri Jun 10 '14 at 12:57
  • http://stackoverflow.com/a/18304605/520567 -- i.e. -c color.status=always for forced colors on git status – akostadinov Sep 22 '14 at 17:34
  • 19
    And if you forgot to specify -r option, you can just type "-r" at the less prompt. This of course works with most or all less options (i.e., -i to turn on ignorecase). – haridsv Jan 07 '16 at 09:32
  • 12
    grep --color=always works the same way. This is not part of this question or answer, but I got here from googling about that question so there it is. – Frank Bryce Dec 06 '16 at 14:37
  • Also works with ack by specifying ack --color. Thanks! – Gargravarr Mar 21 '18 at 15:54
  • I have alias diff='/usr/bin/diff --color=always ' and alias less='/usr/bin/less -r ' and the output is initially coloured but after paging it goes back to mono. – NeilG Nov 02 '19 at 04:06
  • This does not work for git status. For that one should use git config --global color.status always. – Niek Nov 14 '19 at 07:18
  • I found a much easier way for git: using the --paginate option - e.g. git --paginate diff or git --pagine status -vv – xeruf Mar 28 '20 at 12:55
  • Doesn't work for git status – Nicolay77 Apr 23 '20 at 16:38
  • 4
    I much prefer -R because it only effects color control characters. With -r even newlines are always printed literally so -S no longer works – quuxman May 11 '20 at 22:15
  • In ~/.bashrc put alias less='less -r'. – mcp Sep 24 '20 at 15:29
  • It's sad that I want to use cut instead of less. Can less specify how many columns to print? – Kindred Apr 22 '22 at 13:20
  • you should use -R because -r will not work correctly when scrolling.. it will not keep the order of escape sequences and your terminal will end up screwed. – Johannes Schaub - litb Dec 13 '23 at 15:13
81

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.
  • 18
    Using Git 2.11, I found that color.ui true did not work with a pager, but color.ui always did. This may have changed since the answer was posted. – Tom Zych Jan 02 '17 at 14:18
  • 4
    This option (to enable colors and use 'less -r' as your pager by default) with color.ui true do work for comands like git -p diff and git -p status (where -p means pipe all output into $PAGER, by default it's less) even for rather old versions of git (for example, Git 1.7.1). But you still need color.ui always (which tells git to output color codes "even if the output is a pipe (not a tty)") to get colored output when you directly specify git status | less -r or git diff | less -r. When you specify output redirection explicitly, you won't see the output after quit from pager. – kenichi Jan 19 '18 at 17:06
  • git status | less and git -p status seem to behave exactly the same for me when color.ui always is set - but since the former both requires a discouraged configuration option and is longer, I'll simply go with the latter ^^ – xeruf Mar 28 '20 at 13:01
42

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'
enzotib
  • 51,661
30

In case anyone is interested in paging a json with jq and less it can be achieved using:

jq -C <jq args> file.json | less -R

e.g.

jq -C . file.json | less -R

Source: https://github.com/stedolan/jq/issues/764#issuecomment-95355331

dimid
  • 627
27

Also tree has an option to force colors on:

tree -C | less -r

And so on for ls:

ls -lR --color | less -r
19

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 -r may cause 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

Naheel
  • 291
17

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)

E.g., as I use it in my .bashrc:

export LESS=-Xr

The X stops the screen from clearing when exiting less.

Pablo A
  • 2,712
MortenSickel
  • 1,371
  • 1
  • 12
  • 24
3

I believe the ultimate solution is to use unbuffer which can be found in the expect package.

In essence, you retain git diff color by running the command as follows:

unbuffer git --no-pager diff [some_file] | less -r

You have to use --no-pager as otherwise the command hangs.

I'd have liked to take credit for this but jcubic got there long before me.

AnthonyK
  • 569
0

I was looking for a way to achieve this for yq to parse YAML files because I have to work with large YAML files on a daily basis. A quick look at the man yq confirmed that -C flag is also available for yq. The following worked for me.

kustomize build k8s/overlays/dev | yq eval -C | less -r

I added and alias in my ~/.zshrc file for convenience

alias yqlc='yq eval -C | less -r'

Now

kustomize build k8s/overlays/dev | yqlc

works like a charm.

0

Thanks @gerald-senarclens-de-grancy

Gerald answer

Helped me a lot. I only change less -rby more, this keep changes on terminal.

git config --global color.ui true
git config --global core.pager 'more'

This results in

[color]
    ui = true
[core]
    pager = more

0

If you're not getting colored output from less, you might be using the one from busybox. Install the real less instead.