When I read a file in Linux with the command less or more, how can I get the content in colors?

  • 6
    This seems related: superuser.com/questions/36022/less-and-grep-color - does it help?
    – Jonik
    Commented Mar 9, 2010 at 13:40
  • 20
    The title of this question is very misleading. Many people landing on this page expect a solution to the coloring issue you will get when piping a command with colored output to less: colors are lost. (The answers to that ”piping issue“ involve less -R, unbuffer etc.) But the actual question refers to opening a file! — The ambiguity lies primarily in the question's title, but even besides that, IMHO the question is still too broad: ”read a file“ could refer to any file (probably plain text). (well, ”get the content in colors“ is probably referring to syntax highlighting.)
    – myrdd
    Commented Dec 3, 2018 at 23:05
  • I need to correct myself, only 3 (or 4) of 14 answers are missing the OP's actual question: the answers by ChristopheD, Puneet and Onlyjob; and maybe jbbr. Still, two of those answers are part of the three highest-voted ones.
    – myrdd
    Commented Dec 3, 2018 at 23:38
  • 2
    @myrdd It might make most sense for the question to be closed as "unclear what is being asked" since questions asking any of the two would likely get marked as duplicates of this... (And answers for any of the two can get downvoted based on how the voter interprets it...) (My use case was a log file that (annoyingly) has color escape codes in it) Commented Jul 7, 2019 at 12:36
  • 1
    Seems like duplicate of superuser.com/q/71588/105108.
    – ks1322
    Commented Apr 2, 2022 at 12:26

15 Answers 15


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

case "$1" in
        pygmentize -f 256 "$1";;

        pygmentize -f 256 -l sh "$1";;

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

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.

  • 6
    If you want to have coloring of the source code files, you also need to make ~/.lessfilter executable by running chmod u+x ~/.lessfilter. You also need to have pygmentize (pygments.org/download) installed. Commented Dec 18, 2012 at 11:07
  • 8
    @puk you can do something like ls --color=always -l | less -R. Obviously a lot to type but you could alias it to something like ll. That is if you don't want to use any extra libraries.
    – PhilT
    Commented Jul 23, 2014 at 16:17
  • 2
    added @SergiyByelozyorov's comment into the answer.
    – andrybak
    Commented Jan 18, 2015 at 12:54
  • 2
    My edit was rejected so I guess I'll post it as a comment instead: Don't test the exit codes of commands indirectly. You can use if grep -q "#\!/bin/bash" "$1" (the -q suppresses standard output). You may want to redirect standard error with 2>/dev/null.
    – Tom Fenech
    Commented Oct 23, 2015 at 13:16
  • 5
    To get a list of all unique file extensions supported by your currently installed pygmentize version, in a format suitable for pasting into this .lessfilter script, run pygmentize -L | grep -o "(filenames .*)" | sed -E "s,\(filenames (.*)\),\1,gm;s/, /\n/g" | sort -u | tr "\n" "|". Note that on certain Linuxes, setting LESSOPEN may not be necessary because it is already setup to use lesspipe which detects the .lessfilter file already (run echo $LESSOPEN to check).
    – Bart
    Commented May 17, 2019 at 9:38

Try the following:

less -R

from man less:

-r or --raw-control-chars

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


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

  • 21
    This is useful when the file itself contains the escape codes that will need to be displayed.
    – Nitrodist
    Commented Dec 16, 2011 at 21:16
  • 65
    It should be noted that most programs use the isatty(2) syscall to check whether their standard output is a terminal, and usually disable colorized output if it is not. For any pipe to less, isatty will return 0. To check whether this works, try echo -e '\x1b[32;1mtest\x1b[m' | less -r
    – mic_e
    Commented Sep 24, 2013 at 22:53
  • 13
    This answer does not excel in the actually does something test. Commented May 9, 2014 at 22:24
  • 23
    You can also type -R when you already opened less to achieve this.
    – Scz
    Commented Sep 1, 2016 at 7:56
  • 9
    This worked for me with grep only when I included the --color=always option in grep.: grep --color=always foo myfile.txt | less -R
    – Dannid
    Commented Jan 14, 2019 at 17:31

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.

  • 6
    Nice, thanks. Except that I need to use -R as an option to less, as well.
    – naught101
    Commented May 8, 2012 at 6:41
  • 10
    I believe grep -R is for specifying recursive search. less -R is necessary for less to correctly spit the colors back out. grep --color=always [grep cmds] | less -R works for me on OS X 10.7.3!
    – Steven Lu
    Commented May 9, 2012 at 13:56
  • 2
    Is there anyway to let grep know just pipe less -R command and then just do coloring? So, we don't have to put --color=always and less -R all the time.
    – A-letubby
    Commented Feb 27, 2015 at 7:50
  • 1
    This is by far the simplest working answer. Thanks! Commented Oct 3, 2015 at 11:16
  • 2
    This also works when you need to pipe git diff to less. Doing just this: git diff | less won't show you any colors. You need to do this instead: git diff --colors=always | less or git diff --colors=always some_file | less. (I'm using cygwin on Windows 10, by the way.)
    – CSCH
    Commented Aug 27, 2018 at 20:17

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).

  • 14
    Note that you may need to add view - when piping
    – user45909
    Commented Mar 2, 2015 at 0:54
  • 11
    vim is an editor, which loads the complete file into memory, whereas less is a pager, loading the file only partially into memory. You will know the difference with huge files.
    – sjas
    Commented Jul 20, 2016 at 12:13
  • 1
    @RiccardoGalli - Cool idea, but i would agree about the performance concern, it's not instantaneous. When viewing huge logs or greps, especially over-the-line (SSH), less is faster since it's not dumping the entire output line by line via "inserts" into vim. Also, you can search with less using '/'. Additionally it has a "tail" mode using shift-F which is handy.
    – dhaupin
    Commented Sep 29, 2016 at 17:49
  • 1
    gmic -h | view starts to execute vim commands. It is not safe. gmic -h | view - doesn't color on Ubuntu 18.10, so downvoting. Commented Sep 24, 2018 at 11:39
  • 2
    ls --color=always > /tmp/file && view /tmp/file certainly does not work.... It shows the escape codes instead of rendering them. Commented Jul 5, 2019 at 10:24

To tell less to show colors call it with -R:

less -R

Unfortunately some programs detect that their stdout is not a terminal and disable colors - e.g pacman (Arch Linux package manager).

In those cases its possible to use unbuffer:

unbuffer <command> | less -R

Example using pacman

unbuffer pacman -Ss firefox | less -R

The unbuffer command is usually part of the package expect (Arch Linux, Debian/Ubuntu) or expect-dev (legacy versions of Debian/Ubuntu).

To answer the question for completeness:

As others already answered, pygmentize is great for colorizing source code. It does not require unbuffer. Easiest call:

pygmentize someSource.cpp | less -R
  • 5
    To use unbuffer on Ubuntu, sudo apt install expect
    – wisbucky
    Commented Dec 4, 2018 at 18:37
  • 4
    This answer needs more upvotes. Commented Apr 19, 2019 at 14:26
  • I was trying to pipe dmesg output to check on boot errors but the colours didn't work unless I use unbuffer, which was confusing the heck out of me: unbuffer dmesg | less -R works as expected.
    – pbhj
    Commented May 12, 2019 at 9:49
  • 1
    on macOS brew install expect gets you the necessary unbuffer command.
    – luckman212
    Commented Oct 19, 2019 at 20:30
  • On macOS, the brew install expect installed doesn't work, maybe because I have conda installed. There seems to be no expect installable from conda that could fix this unfortunately. See: askubuntu.com/questions/1047900/… Commented May 10, 2022 at 14:47

pygmentize supports the -g option to automatically guess the lexer to be used which is useful for files read from STDIN without checking any extension type.

Using that, you only need to set the following 2 exports in your .bashrc without any additional scripts:

export LESS='-R'
export LESSOPEN='|pygmentize -g %s'
  • 4
    Concise and effective. I prefer defining an alias, because sometimes less is better. So: alias lesc='LESS="-R" LESSOPEN="|pygmentize -g %s" less' Commented Apr 28, 2014 at 18:27

You didn't say what this color should mean, e.g. what should the colors be for a text file?

If what you want is syntax highlighting for source code, you need a source code highlighter. I sometimes use pygmentize like this

pygmentize file.cpp | less


pygmentize file.cpp | more

There are other highlighters around.

This is pretty fast. If you don't mind firing up vim there is a read-only mode that can give you syntax highlighting if you have it in vim.

view file.cpp

or alternatively see churnd's answer.


This is yet another pygments-based answer, with several major improvements:

  • does not break lesspipe or lessfile filters
  • works with multiple inputs to less
  • correctly parses the script type from the shebang header
  • works for all 434 file types lexable by Pygments
  • color scheme is parameterized as an environment variable

EDIT: I maintain an updated/improved version of this script here: https://github.com/CoeJoder/lessfilter-pygmentize

Original version below:

Install Pygments and Gawk

sudo apt-get install python-pygments python3-pygments gawk

Set Environment Variables

Check whether lesspipe or lessfile is already enabled:


If you don't see either program referenced there, ensure that lesspipe is installed (most distros come with it).

Add the following to ~/.bashrc:

# sets LESSOPEN and LESSCLOSE variables
eval "$(SHELL=/bin/sh lesspipe)"

# interpret color characters
export LESS='-R'

# to list available styles: `pygmentize -L styles`
export PYGMENTIZE_STYLE='paraiso-dark'

# optional
alias ls='ls --color=always'
alias grep='grep --color=always'

If you don't want lesspipe, replace the eval statement with:

export LESSOPEN='|~/.lessfilter %s'

Create ~/.lessfilter

Add the following code and make the file executable: chmod u+x ~/.lessfilter

for path in "$@"; do
    # match by known filenames
    filename=$(basename "$path")
    case "$filename" in
            # shell lexer
            pygmentize -f 256 -O style=$PYGMENTIZE_STYLE -l sh "$path"
            # filename recognized
            pygmentize -f 256 -O style=$PYGMENTIZE_STYLE "$path"
            ext=$([[ "$filename" = *.* ]] && echo ".${filename##*.}" || echo '')
            case "$ext" in
                    # extension recognized
                    pygmentize -f 256 -O style=$PYGMENTIZE_STYLE "$path"
                    # parse the shebang script header if it exists
                    lexer=$(head -n 1 "$path" |grep "^#\!" |awk -F" " \
'match($1, /\/(\w*)$/, a) {if (a[1]!="env") {print a[1]} else {print $2}}')
                    case "$lexer" in
                            # workaround for lack of Node.js lexer alias
                            pygmentize -f 256 -O style=$PYGMENTIZE_STYLE \
                                -l js "$path"
                            exit 1
                            pygmentize -f 256 -O style=$PYGMENTIZE_STYLE \
                                -l $lexer "$path"
exit 0
  • 1
    TIL: If you get an error like "awk: line 1: syntax error at or near ," with the above .lessfilter in place, check that gawk is installed.
    – Bryce
    Commented Oct 23, 2019 at 23:38

Use the GNU Source-highlight; you can install it with apt if you have it, or otherwise install it from source. Then set up an "input preprocessor" for less, with help from the Source-highligh' documentations for setting up with less:

This was suggested by Konstantine Serebriany. The script src-hilite-lesspipe.sh will be installed together with source-highlight. You can use the following environment variables:

 export LESSOPEN="| /path/to/src-hilite-lesspipe.sh %s"
 export LESS=' -R '

This way, when you use less to browse a file, if it is a source file handled by source-highlight, it will be automatically highlighted.

Xavier-Emmanuel Vincent recently provided an alternative version of ANSI color scheme, esc256.style: some terminals can handle 256 colors. Xavier also provided a script which checks how many colors your terminal can handle, and in case, uses the 256 variant. The script is called source-highlight-esc.sh and it will be installed together with the other binaries.


To expand upon another answer, you can make it work for most if not all of your scripts that don't have extensions by changing the .lessfilter file around just a bit:

    case "$1" in
        pygmentize -f 256 "$1";;
        pygmentize -f 256 -l sh "$1"
        scriptExec=$(head -1 "$1" |grep "^#\!" |awk -F" " '{print $1}')
        if [ "$scriptExecStatus" -eq "0" ]; then
            lexer=$(echo $scriptExec |awk -F/ '{print $NF}')
            pygmentize -f 256 -l $lexer "$1"
            exit 1

exit 0

You'd still need to add the two variables to .bashrc:

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

And you'll still need to make .lessfilter executable:

$ chmod 700 ~/.lessfilter

Also I wanted to add that under debian the pygments package is called python-pygments. I had trouble locating it at first because the obvious misspelling of "pigments" as "pygments" wasn't enough of a hint to me that it was a package that might be prefixed with "python-" by the package manager.

  • 2
    2 comments: 1) Thanks for the improvement. 2) Phrases like "voted best answer" aren't great; that may change (in fact, if this is better than that answer, this post might become the top answer, at which point it'll just be confusing. Maybe just say "to expand upon another answer" or "captaincomic's answer"?
    – cpast
    Commented Feb 27, 2013 at 23:07

Condensed from my full blog post about improving less experience: https://www.topbug.net/blog/2016/09/27/make-gnu-less-more-powerful/

For colorful manpages, add the following to your .bashrc or .zshrc:

export LESS_TERMCAP_mb=$'\E[1;31m'     # begin bold
export LESS_TERMCAP_md=$'\E[1;36m'     # begin blink
export LESS_TERMCAP_me=$'\E[0m'        # reset bold/blink
export LESS_TERMCAP_so=$'\E[01;44;33m' # begin reverse video
export LESS_TERMCAP_se=$'\E[0m'        # reset reverse video
export LESS_TERMCAP_us=$'\E[1;32m'     # begin underline
export LESS_TERMCAP_ue=$'\E[0m'        # reset underline

For syntax highlighting, using an existing powerful lesspipe.sh to handle it instead of writing your own: https://github.com/wofr06/lesspipe


An alternative to less/more that works with colors out of the box is bat. You can install it with most package managers use it as a pager as well as a cat replacement.


  • Very fast and straigthforward !
    – abu_bua
    Commented Mar 1, 2022 at 23:57
  • bat behaves like cat when the content is small enough to fit inside your window; that is, writing to terminal without opening a pager. You can use the option bat --paging=always to always use a pager.
    – RexYuan
    Commented Nov 4, 2022 at 16:39

You can consider using most utility which is colour-friendly alternative for less and more.

  • can you show us one example? I tried here, and the output was black and white.
    – danilo
    Commented Jun 13, 2019 at 17:47
  • Your input should contain colours. First produce a colorised sample (e.g. ccze -A </var/log/dpkg.log, ls -1 --color /var/log) then pipe it to most: ls -1 --color /var/log | most.
    – Onlyjob
    Commented Jun 15, 2019 at 0:06
  • yes, I used: git status | less --color, git status | most --color
    – danilo
    Commented Jun 15, 2019 at 0:14
  • I used most, more, less, and all tools show black and white
    – danilo
    Commented Jun 15, 2019 at 0:17
  • Make sure that your command produces colours before piping to less or others. Make sure your terminal emulator can output colours. Check TERM environment variable. Read more in unix.stackexchange.com/questions/148/… When possible use modern GNU+Linux distribution like Debian. Use search engine (e.g. duckduckgo.com startpage.com) to find answers. Remember that comments are not for discussion.
    – Onlyjob
    Commented Jun 16, 2019 at 5:06

I found this simple elegant solution. You don't have to install anything extra as it is already there by default on most machines. As vim is installed by default on most machines, it includes a macro to run vim like less

Some of the options to use it are to create an alias: alias vless='vim -u /usr/share/vim/vim74/macros/less.vim'

or create a symbolic link: ln -s /usr/share/vim/vim74/macros/less.sh ~/bin/vless

Then you just run vless myfile.py

I got most of the information here

  • 1
    I have alias lesser='/usr/share/vim/vim80/macros/less.sh' in ~/bash_aliases (in Ubuntu 18.04). Can use shortcuts such as f forward, b backward, d half down, u half up, q quit, etc...
    – Daniel
    Commented Jun 5, 2019 at 14:01

Another way to make less display more colors is --use-color. This is non-syntax highlighting. For example, running

LESS='--use-color -RNJWj5 --header=2' man less

will, after trying a few commands (searching, scrolling, marking, etc.), result in

enter image description here

--use-color enables colored text

--header=2 -RNJWj3 enables various features that employ colors

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .