Can I get less not to monochrome its output?

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

up vote 297 down vote accepted

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
  • 2
    @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
  • 2
    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 9:19
  • 17
    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
  • 3
    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 7 '16 at 9:32

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.

  • 3
    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 2 '17 at 14:18
  • 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 at 17:06

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'
  • 5
    Actually I just did alias less='less -r'. Any reason not to? – ripper234 Aug 24 '11 at 12:25
  • 3
    @ripper234: I vaguely remember there could be cases when using -r when not needed can lead to visualization problem. – enzotib Aug 24 '11 at 12:29
  • 1
    For instance, when you pipe ack with less -r you lose a carriage return – New Alexandria Jan 14 '13 at 15:58
  • 6
    You can just use \less if visualization messes up. – Brian Peterson Oct 26 '13 at 0:41
  • 1
    Uh, you don’t have to alias less to anything, just set the $LESS environment variable to the set of flags you always want to provide to less, e.g. export LESS='-MRq -z-2 -j2’ – wjv Aug 30 '17 at 19:28

Also tree has an option to force colors on:

tree -C | less -r

And so on for ls:

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

  export LESS=-Xr

(The X stops the screen from clearing when exiting less)

  • 1
    unfortunately -X also breaks mouse scrolling for me :c – Xerus Apr 17 at 11:27

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

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.