How do I make `less` output colors?

34,116

Solution 1

There are two problems here:

  • Commands like ls —which auto-detect the colour support— don't find support from pipes
  • less is set to just display colour codes by default.

Both can be overcome but it's a bit clunky:

ls --color=always | less -R

This isn't ls specific. Many commands that support colour also have an override argument.


A slightly more in-depth answer is that ls is checking whether or not its STDOUT belongs to a real terminal or not. When you pipe things around, the STDOUT is set to the STDIN of the next command.

You can see this at work in the ls source code. It's using the isatty command (a core POSIX interface) to work out what the situation is:

  • Are colours on by default:

        print_with_color = (i == color_always
                            || (i == color_if_tty
                                && isatty (STDOUT_FILENO)));
    
  • Do we try to output in multiple columns:

    if (format == long_format)
      format = (isatty (STDOUT_FILENO) ? many_per_line : one_per_line);
    
    //...
    
    if (isatty (STDOUT_FILENO))
      {
        format = many_per_line;
        set_quoting_style (NULL, shell_escape_quoting_style);
        qmark_funny_chars = true;
      }
    else
      {
        format = one_per_line;
        qmark_funny_chars = false;
      }
    

grep does a very similar thing, unless explicitly overridden, it'll detect colour support, with isatty:

color_option = isatty (STDOUT_FILENO) && should_colorize ();

Solution 2

If you're interested in colors in less more generally, you might want to look at lesspipe.sh. See, for example, https://github.com/wofr06/lesspipe.

lesspipe.sh is an input filter for the pager less as described in less's man page. The script runs under a ksh-compatible shell (e.g. bash, zsh) and allows you to use less to view files with binary content, compressed files, archives, and files contained in archives.

It will also colorize shell scripts, perl scripts, etc. similarly to a text editor, but without the use of any "preprocessing" program to do the colorizing.

Share:
34,116

Related videos on Youtube

Mohammad Reza Rezwani
Author by

Mohammad Reza Rezwani

Msc of computer networks.

Updated on September 18, 2022

Comments

  • Mohammad Reza Rezwani
    Mohammad Reza Rezwani almost 2 years

    When I run a program (for example grep or ls) without a pager, its output is colored. However when I run it piping its output to less, no colors are shown.

    For example, this command outputs colored output:

    grep -r something
    

    but this doesn't:

    grep -r something | less
    

    Why? How can I see colors through less?

    • Rinzwind
      Rinzwind about 10 years
      grep --color=always "search string" * | less -R but I tend to use most nowadays instead of less. ALSO: gnu.org/software/src-highlite is a color highlighter (less works too)
    • sourav c.
      sourav c. about 10 years
      @alex have you installed python-pygments?
    • Mohammad Reza Rezwani
      Mohammad Reza Rezwani about 10 years
      @souravc no I did not install that
    • Rinzwind
      Rinzwind about 10 years
      @ales ignoring me are we? >:-D
    • Pablo Bianchi
      Pablo Bianchi over 2 years
      Check here (or here) if what you want is just syntax highlight on less
  • hytromo
    hytromo about 10 years
    My last upvote (reached the limit) for today to support you for 100k. But me and Rinzwind want cake when that happens.
  • Oli
    Oli about 10 years
    @alex ack has a --color argument: ack-grep -i select --color | less -R
  • Mohammad Reza Rezwani
    Mohammad Reza Rezwani about 10 years
    @Oli .........Oh my God........ the only thing that I can say about your knowledge and help and also power of linux
  • Rinzwind
    Rinzwind about 10 years
    @ales he stole that line from my comment >:-D
  • Oli
    Oli about 10 years
    @Rinzwind I'd like to say "BWAHAHAAA!" but I didn't even notice :(
  • Rinzwind
    Rinzwind about 10 years
    @oli ok but hakermania and me want cake in 1900 rep >:D
  • Braden Best
    Braden Best over 8 years
    I still wonder how ls does its output. Not the colors, I'm talking how it displays the files in columns to optimally fill the whole width of the terminal, and then the second you pipe it to something, it's a straight list. That's some ingenious engineering.
  • Oli
    Oli over 8 years
    @B1KMusic I agree. I agree so much that I found how how it works and updated the answer. You can force columns with a -C argument.
  • vstepaniuk
    vstepaniuk over 4 years
    For me it doesn't work: printf "a\nb\n" > text; pcregrep -M --color=always "a\nb\n" text | less -R highlights only "a", but should both "a" and "b".