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


9 Answers 9


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.
    – eexpress
    Apr 1, 2017 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, 2016 at 15:08

You can use bat: https://github.com/sharkdp/bat

bat your_file

acts as cat but pretty-prints the file contents, including file name, line numbers and syntax highlighting. There are options for piping colorized output e.g. to less. By default, when the output is piped, it behaves like cat and just copies the data from stdin to stdout.

Unlike GNU source-highlight which uses the file extension for language recognition, bat recognizes the syntax from the input itself and therefore can read piped input without explicitly specifying the language.


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


You can use the package, e2ansi, that provides syntax highlighting support for pagers like more and less.

The package use the mother of all text editors, Emacs, to perform the actual syntax highlighting. As an added bonus, all other conversions normally performed by Emacs -- like uncompressing files -- is also performed.


The following is the result of viewing a file using less and e2ansi:



The package provides a command-line tool e2ansi-cat that starts Emacs in batch mode, opens files, syntax highlight them, and renders the result using ANSI sequences.

You can integrate this into less by setting the following variables to, for example (the location of your init file may vary):

export "LESSOPEN=||-/usr/local/emacs --batch -Q -l ~/.emacs -l bin/e2ansi-cat %s"
export "LESS=-r"
alias "more=less -X -E"

In the configuration above, less restores the original terminal window content whereas more simply output new content after the prompt.

Note: If you use an old version of less, it might not support the || or the - syntax, in which case you may need to use simply LESSOPEN=|/usr/local/emacs ....

Using less in pipes

The "-" character in LESSOPEN indicates that the input filter should also be used when piping text into less. In this case, Emacs can only rely on text itself (and not a file name). Fortunately, Emacs provides a system for this. In addition, the provided file file e2ansi-magic.el sets up additional file types. For example:

Pipe example

Why use Emacs?

  • Emacs has support for virtually all programming languages and structured text formats. In most cases, the syntax highlighting support is excellent.
  • You can easily add support for more languages and formats, or modify existing packages to suit you needs.
  • Emacs support color themes. When using e2ansi, the colors in the theme is preserved when viewing a file in less. You can pick a suitable color theme from a number of sources, or design your own.
  • If you use Emacs as your editor of choice, you will get the same highlighting in the editor as you get when viewing a file using less (minus limitations in ANSI sequence format and the terminal window).
  • less will take advantage of Emacs features that perform automatic conversion, for example uncompressing files. In fact, you can teach Emacs to perform any kind of conversion like automatically convert a binary file to human readable form using an external tool.
  • You can view files located on other machines by using Emacs' syntax for remote access like /USER@HOST:FILENAME.

Useful links

  • e2ansi is distributed on Melpa and can be installed using the standard Emacs package system
  • e2ansi is hosted on GitHub
  • The e2ansi page on the Emacs Wiki
  • The home of less.

Operating system notes

  • On MS Windows, the console does not natively support ANSI sequences. Fortunately, the less application is capable of rendering them. I am not aware of any contemporary binary distribution of less for MS Windows and the provided build files is hard to use. Fortunately, it's easy to build less using CMake, see this text for details.

  • OS X distributes an ancient version of less. Fortunately, it's easy to build a modern version directly from the source.


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.


Themes, to use with the -s param:


highlight -l --out-format=html -s neon --syntax=php < in.txt 1> out.html

List themes available on your system:

ls /usr/share/highlight/themes

acid.theme         darkbone.theme           edit-matlab.theme     maroloccio.theme    rdark.theme
aiseered.theme     darkness.theme           edit-msvs2008.theme   matrix.theme        relaxedgreen.theme
andes.theme        darkslategray.theme      edit-nedit.theme      moe.theme           rootwater.theme
anotherdark.theme  darkspectrum.theme       edit-purebasic.theme  molokai.theme       seashell.theme
autumn.theme       denim.theme              edit-vim-dark.theme   moria.theme         solarized-dark.theme
baycomb.theme      dusk.theme               edit-vim.theme        navajo-night.theme  solarized-light.theme
bclear.theme       earendel.theme           edit-xcode.theme      navy.theme          sourceforge.theme
biogoo.theme       easter.theme             ekvoli.theme          neon.theme          tabula.theme
bipolar.theme      edit-anjuta.theme        fine_blue.theme       nightshimmer.theme  tcsoft.theme
blacknblue.theme   edit-bbedit.theme        freya.theme           night.theme         the.theme
bluegreen.theme    edit-eclipse.theme       fruit.theme           nuvola.theme        vampire.theme
breeze.theme       edit-emacs.theme         github.theme          olive.theme         whitengrey.theme
bright.theme       edit-fasm.theme          golden.theme          orion.theme         xoria256.theme
camo.theme         edit-flashdevelop.theme  greenlcd.theme        oxygenated.theme    zellner.theme
candy.theme        edit-gedit.theme         kellys.theme          pablo.theme         zenburn.theme
clarity.theme      edit-godot.theme         leo.theme             peaksea.theme       zmrok.theme
dante.theme        edit-jedit.theme         lucretia.theme        print.theme
darkblue.theme     edit-kwrite.theme        manxome.theme         rand01.theme

Themes preview View as html

highlight linux all themes

All theme preview generated with the below php code:

$themes = explode(".theme", `ls /usr/share/highlight/themes`);
foreach ($themes as $k=>$theme){
  $theme = trim($theme);
  system("echo '<hr><h3>$theme</h3>' >> all.html && highlight -l --out-format=xhtml --inline-css -s $theme --syntax=php < snippet.php 1>> all.html");

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, 2016 at 13:44

You must log in to answer this question.

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