The Computer Oracle

When reading a file with `less` or `more`, how can I get the content in colors?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Darkness Approaches Looping

--

Chapters
00:00 When Reading A File With `Less` Or `More`, How Can I Get The Content In Colors?
00:15 Answer 1 Score 591
00:39 Answer 2 Score 167
01:20 Accepted Answer Score 293
03:16 Answer 4 Score 47
03:37 Thank you

--

Full question
https://superuser.com/questions/117841/w...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux #colors #less

#avk47



ANSWER 1

Score 591


Try the following:

less -R

from man less:

-r or --raw-control-chars

Causes "raw" control characters to be displayed. (...)

-R or --RAW-CONTROL-CHARS

Like -r, but only ANSI "color" escape sequences are output in "raw" form. (...)




ACCEPTED ANSWER

Score 293


If you just want to tell less to interpret color codes, use less -R. ref.


You can utilize the power of pygmentize with less - automatically! (No need to pipe by hand.)

Install pygments with your package manager or pip (possibly called python-pygments) or get it here http://pygments.org/download/.

Write a file ~/.lessfilter

#!/bin/sh
case "$1" in
    *.awk|*.groff|*.java|*.js|*.m4|*.php|*.pl|*.pm|*.pod|*.sh|\
    *.ad[asb]|*.asm|*.inc|*.[ch]|*.[ch]pp|*.[ch]xx|*.cc|*.hh|\
    *.lsp|*.l|*.pas|*.p|*.xml|*.xps|*.xsl|*.axp|*.ppd|*.pov|\
    *.diff|*.patch|*.py|*.rb|*.sql|*.ebuild|*.eclass)
        pygmentize -f 256 "$1";;

    .bashrc|.bash_aliases|.bash_environment)
        pygmentize -f 256 -l sh "$1";;

    *)
        if grep -q "#\!/bin/bash" "$1" 2> /dev/null; then
            pygmentize -f 256 -l sh "$1"
        else
            exit 1
        fi
esac

exit 0

In your .bashrc (or .zshrc or equivalent) add

export LESS='-R'
export LESSOPEN='|~/.lessfilter %s'

Also, you need to make ~/.lessfilter executable by running

chmod u+x ~/.lessfilter

Edit: If you have lesspipe on your system, you might want to use that to automatically unzip archives when looking at them with less, e.g. less log.gz. lesspipe also supports a custom .lessfilter file, so everything said above works the same, you just have to run

eval "$(lesspipe)" 

in your rc file instead of setting the LESSOPEN variable. Run echo "$(lesspipe)" to see what it does. Your .lessfilter will still work. See man lesspipe.


Tested on Debian.

You get the idea. This can of course be improved further, accepting more extensions, multiple files, or parsing the shebang for other interpreters than bash. See some of the other answers for that.

The idea came from an old blog post from the makers of Pygments, but the original post doesn't exist anymore.


Btw. you can also use this technique to show directory listings with less.




ANSWER 3

Score 167


I got the answer in another post: Less and Grep: Getting colored results when using a pipe from grep to less

When you simply run grep --color it implies grep --color=auto which detects whether the output is a terminal and if so enables colors. However, when it detects a pipe it disables coloring. The following command:

grep --color=always "search string" * | less -R

Will always enable coloring and override the automatic detection, and you will get the color highlighting in less.

Warning: Don't put --color=always as an alias, it break things sometimes. That's why there is an --color=auto option.




ANSWER 4

Score 47


Use view instead of less. It opens the file with vim in readonly mode.

It's practically a coloured less: a pager where you can search with / (and more). The only drawback is that you can't exit with q but you need :q

Also, you get the same colouring as vim (since you're in fact using vim).