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
|improve this answer|||||
  • 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.

|improve this answer|||||

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.

|improve this answer|||||
  • 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

|improve this answer|||||

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.

|improve this answer|||||

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.

|improve this answer|||||

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)

|improve this answer|||||
  • 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.