Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

is there a way to syntax highlight a file I see in Less?

actually I'm using this command to open an xml file (and sometimes a series of them)

less htmleditors/htmleditors_config.xml


less [multiple files]

I'd like to stay in Less (to learn that program better and to use my knowledge of :n and :p for next/previous navigation)

But it also want some kind of basic syntax highlighting – at least show the comments differently. Do you know any way to do it?

share|improve this question

12 Answers 12

up vote 77 down vote accepted

You can use GNU's source-highlight, as shown here:

 export LESSOPEN="| /usr/bin/ %s"
 export LESS=' -R '
share|improve this answer
I retrieve the following error message when I run less ~/.zshrc. Error: line 9: source-highlight: command not found. I run this on MacOSX. – JJD Jun 4 '12 at 16:38
@JJD Did you install source-highlight? – Max Nanasy Aug 31 '12 at 6:00
OSX: brew install source-highlight; export LESSOPEN="| /usr/local/bin/ %s". Note the path change. – Gregg Lind Apr 22 '14 at 19:26
On Debian: sudo apt-get install source-highlight, export LESSOPEN="| /usr/share/source-highlight/ %s", export LESS=' -R ' – Morgan Courbet Aug 22 '14 at 8:57
small caveat: source-highlight does not currently support markdown. – dhulihan Jul 7 at 6:10

Best of both previous answers: you can invoke system default editor from within less, by pressing v.

share|improve this answer
Wow! This is amazingly useful! – dancek Aug 9 '12 at 5:57
Curses that opened Nano on my Ubuntu box! – jamesc Jan 18 '13 at 10:12
Then set $EDITOR or $VISUAL to vim (or emacs, or gedit, or joe, or ed, or mined, or…). – Daniel H Jun 5 '13 at 1:42
@jamesc FYI, you can enable syntax highlighting in Nano, here is how. – Captain Man Apr 14 at 19:00
@CaptainMan Nope nope nope! I change the default back to vim like Daniel H said! :D – jamesc Apr 15 at 15:23
pygmentize somefile.ex | less -R


function cless () {
    pygmentize -f terminal "$1" | less -R

Pygmentize comes as a part of the Pygments Python syntax highliter.

share|improve this answer
pygmentize also works with the LESSOPEN stuff mentioned in other answers here. – Nathan Aug 5 '14 at 19:01
To get pygmentize, you need Python and then you can install pygmentize with pip install pygments. – wkschwartz Mar 20 '15 at 1:11
To get less to use pygmentize for syntax highlighting use: which pygmentize 2> /dev/null >&2 && export LESSOPEN="| pygmentize -g -f terminal256 %s". -g to make it work with pipes (less <(diff -u file1 files)). It's available in community/pygmentize package for arch linux, and in python-pygments for debian. – x-yuri May 16 '15 at 18:38

I think that you should use a text editor. I like vim myself. That will give you LOTS of power when viewing files and then when you want to edit them you will already know the basics.

Here are some of the advantages of using a text editor (specifically vim):

  • syntax-highlighting
  • powerful movement commands
  • find
  • jump to specific location in a file (called a mark)
  • folding (useful when you just want to see function stubbs)

To open your file in readonly mode use this:

vim -R <file name>

Here is a basic navigation guide:

j - move down one line
k - move up one line
h - left one char
l -right one char

ctrl-f - forward one page
ctrl-b - back one page

/<something> - search for something
n - next of whatever you searched for
N - next (search backwards) of whatever you searched for

:q - quit
:q! - quit without saving
:w - save

Here is a link for more information:

Just to recap, if you will use unix vim is pretty fundamental. I have heard that learning vim is like learning to type. It is the next most useful tool you can learn for programming.

(Just to avoid editor wars you could also look into emacs or another editor, however I personally prefer vim)

share|improve this answer
I second this. Vim launches as fast as less, and many of the keyboard shortcuts (e.g., search, next page, previous page) are identical. – Jeremy W. Sherman Feb 10 '11 at 18:20
Just FYI, on a lot of systems, view is an alias for vim -R. – Andrew Ferrier Sep 18 '12 at 13:12

As others have said, you can use the power of vim. But importantly, you can do so without learning how to use vi/vim.

Vim comes with a less.vim script that works pretty well as a replacement for less, with full color syntax highlighting. It uses less keybindings (just hit 'q' to quit).

It had a few problems, so I improved it. I have a screenshot at

share|improve this answer
Awesome! But as I couldn't find the script right away I'd like to share the command for locating it: find /usr/share/vim -name '' which is taken from this gist: – yoshi Feb 21 '15 at 22:13

I was also searching for this and found another solution using Vim:

The post is rather old, so now on more recent distros vim 7.2 is shipped and the .bashrc will read: alias vless='vim -u /usr/share/vim/vim72/macros/less.vim'

share|improve this answer
MacVim comes with a shell script that you can use directly, located in: /Applications/ – Nick Jan 14 '12 at 19:23

If you have GNU Source-highlight installed you can use the following command to highlight the syntax of a single file:

$ yourfile.xml | less -R
share|improve this answer

I'm glad to announce a new 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.

share|improve this answer
For OS X, I'd recommend using the package manager brew [] with brew dupes [] to install up-to-date versions of things like less. – drevicko Jan 26 at 10:53

source-highlight has a .sh script located in /usr/share/source-highlight/*.sh. I've created a symbolic link to it with name hcat in /usr/bin.

It shows highlighted output in terminal (console) – hcat is highlighted cat.

How to:

[me@this]<bash><~> 43 
21:23 Fri Apr 19 > sudo apt-get install source-highlight

[me@this]<bash><~> 28 
21:03 Fri Apr 19 > ll /usr/share/source-highlight/*.sh
-rwxr-xr-x 1 root root 432 May  1  2012 /usr/share/source-highlight/*

[me@this]<bash><~> 29 
21:04 Fri Apr 19 > sudo su
root@this:/home/me# cd /usr/bin/
root@this:/usr/bin# ln -s /usr/share/source-highlight/ hcat

[me@this]<bash><~> 36 
21:07 Fri Apr 19 > hcat test.xml
share|improve this answer

Using OS X 10.9 (Maverick) this would do the trick:

  • brew install source-highlight (Assuming brew is installed -
  • sudo nano /etc/launchd.conf

    setenv LESSOPEN="| /usr/local/bin/ %s"     
    setenv LESS=' -R '

    (Add to /etc/launchd.conf)

  • reboot

share|improve this answer

In mac, you can just type view filename, does similar thing like less.

share|improve this answer
With OSX, view filename just launches vim by default - slightly different key bindings to less (see other answers to this question for more details). – drevicko Jan 26 at 10:56

After installing GNU's source-highlight (package dev-util/source-highlight on sabayon/gentoo), I configured those envs in /etc/bash/bashrc.d/my-less-src-highlight:

#default:    export LESSOPEN="|lesspipe %s"
#don't like: export LESSOPEN="| /usr/bin/ %s"
#default:    export LESS=" -R -M --shift 5"
export LESSCOLOR=always
export LESSCOLORIZER=/usr/bin/

I did not like to replace lesspipe because that has other features. Concatenation did not work.

share|improve this answer

You must log in to answer this question.

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