4

I'm working remotely on a SLES11 machine (woe is me). On this machine, I'm using git, and specifically, git diff, which passes its results to less with some of colorization. Now, for some reason, instead of seeing color, I'm seeing lines which look like:

ESC[1mdiff --git a/path/to/file.h b/path/to/file.hESC[m
ESC[1mindex 1ab153f..0491db9 100644ESC[m

etc.

I know the terminal supports color (ls results are colorized); I have TERM=xterm and COLORTERM=1 in my environment.

How can I get the colorized diff to display properly?

7

As terdon says, less’ default behaviour is to display equivalents of special characters, in cat -v style. less -R will change that so that escape sequences are passed on to whatever is handling the display.

less’s defaults can be specified with the LESS environment variable, e.g.

export LESS=-R

git has its own idea of what its pager should do. If no LESS environment variable is set, it will set it to FRX when invoking less, which matches git’s expectations; if LESS is set, it will leave it unchanged, which can result in unreadable output if LESS doesn’t include -R.

There are two ways of configuring less for use with git: either configure it globally using the LESS variable, or change the core.pager setting, e.g.

git config --global core.pager "less -R"
3
  • Will configuring the "core pager" this way override other settings in the LESS environment variable, or add to them? – einpoklum Jun 24 at 11:35
  • 1
    @einpoklum it will add to them. The various possibilities are discussed in detail in the documentation I linked. – Stephen Kitt Jun 24 at 11:39
  • 1
    Fun fact: you can also configure LESS settings (and keybindings) with ~/.lesskey, which you "compile" with lesskey(1) into ~/.less so less itself can avoid text parsing on every startup. (Remember, less is old :P). In mine, I have LESS = iMRj5X, since -R is good in general on a colour terminal. – Peter Cordes Jun 24 at 19:40
4

That's the default behavior of less. Use less -R if you want to see colored output (from man less):

   -R or --RAW-CONTROL-CHARS
          Like -r, but only ANSI "color" escape sequences and OSC  8  hy‐
          perlink  sequences  are  output  in "raw" form.  Unlike -r, the
          screen appearance is maintained correctly, provided that  there
          are  no  escape sequences in the file other than these types of
          escape sequences.  Color escape sequences  are  only  supported
          when  the  color  is changed within one line, not across lines.
          In other words, the beginning of each line  is  assumed  to  be
          normal  (non-colored),  regardless  of  any escape sequences in
          previous lines.  For the purpose of keeping track of screen ap‐
          pearance,  these  escape  sequences are assumed to not move the
          cursor.

You're probably used to systems that have less set up as an alias to less -R which is why this caught you off guard.

2
  • 1
    ... if that's the default behavior, shouldn't git invoke less with -R then? – einpoklum Jun 24 at 10:35
  • @einpoklum git-config(1) says "When the LESS environment variable is unset, Git sets it to FRX." So, it kind of does. It mustn't have worked in your case, because you had LESS already set to something else. – JoL Jun 24 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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