Lots of programming-oriented editors will colorize source code. Is there a command that will colorize source code for viewing in the terminal?

I could open a file with emacs -nw (which opens in the terminal instead of popping up a new window), but I'm looking for something that works like less (or that works with less -R, which passes through color escape sequences in its input).


With highlight on a terminal that supports the same colour escape sequences as xterm:

highlight -O xterm256 your-file | less -R

With ruby-rouge:

rougify your-file | less -R

With python-pygments:

pygmentize your-file | less -R

With GNU source-highlight:

source-highlight -f esc256 -i your-file | less -R

You can also use vim as a pager with the help of macros/less.sh script shipped with vim (see :h less within vim for details):

On my system:

sh /usr/share/vim/vim74/macros/less.sh your-file

Or you could use any of the syntax highlighters that support HTML output and use elinks or w3m as the pager (or elinks -dump -dump-color-mode 3 | less -R) like with GNU source-highlight:

source-highlight -o STDOUT -i your-file | elinks -dump -dump-color-mode 3 | less -R
  • Highlight can convert text into SVG/HTML, which is very exciting. – utopic eexpress Apr 1 '17 at 4:02

I use GNU source-highlight combined with less -R for this. Add the following lines to your shell's initialization file (~/.bashrc, for example):

 LESSOPEN='| source-highlight --failsafe --out-format=esc256 -o STDOUT -i %s 2>/dev/null ' less -R "$@"

That should automatically color source code according to the language used and, if it can't, it will launch less on its input directly.


If you're interested in colors in less more generally, you might want to look at lesspipe.sh. See, for example, http://freecode.com/projects/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.

  • Thanks! I think lesspipe.sh is what I was trying to remember before I posted the question... – alexis Mar 3 '16 at 15:08

I enjoy simplicity and I use nano text editor, in this case I usually proceed as follow:

check witch syntax highlight is available: ls -la /usr/share/nano/

include it in my nanorc file with following command, or by doing it manually

echo 'include /usr/share/nano/sh.nanorc' >> ~/.nanorc

about less I suggest to give a look at this page


According to Stéphane Chazelas suggestion, I make a bash function to use highlight show Syntax highlight, put it into .bashrc or so on. It works great, fast and effective.

s(){ highlight --force -O ansi $1 | /usr/bin/less -R; }

highlight support 202 language syntax format. see highlight --list-scripts=langs.


It is called syntax highlighting.

GNU Source-highlight when given a source file, produces a document with syntax highlighting.

I use the lessfilter script below, or you could make less invoke it automatically like with:

export LESSOPEN="| /path/to/lessfilter %s"
export LESS=' -R '

I found this article and started doing it this way:

file -b -L "$1" | grep -q text &&
  /usr/share/source-highlight/src-hilite-lesspipe.sh "$1"

(src-hilite-lesspipe.sh is shipped with source-highlight but not all distributions include it)

  • 4
    "If you go to the article the author must be using a Mac since he uses capitals for his commands": I'm afraid you're a bit confused: Clearly you are not aware that export defines an environment variable, not an alias. The command less uses flags from the environment variable LESS, so your answer needs some work. – alexis Mar 3 '16 at 13:44

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.