77

I need to use the less command with the syntax highlighting of the vim command for python, C, bash and other languages.

How do I apply syntax highlighting colors according to vim colors for less command?

0

5 Answers 5

70

Syntax highlighting of less, works just fine on most *nix systems.

apt install source-highlight
export LESSOPEN="| /usr/share/source-highlight/src-hilite-lesspipe.sh %s"
export LESS=' -R '

On Fedora/RedHat based distros use /usr/bin/src-hilite-lesspipe.sh instead.

Even on Cygwin you can do it with the minor adjustment of the shell script path and installing with apt-cyg instead of apt.

However, using this drastically slows down browsing of large files. I suggest to use alias in such a way to only implement the LESSOPEN export above when needed, like this:

alias lessh='LESSOPEN="| /usr/bin/src-hilite-lesspipe.sh %s" less -M '

where the -M flag is convenient to also show filename and line number.

Also remember to copy the script into your bin path:

cp /usr/share/source-highlight/src-hilite-lesspipe.sh /usr/bin/src-hilite-lesspipe.sh

UPDATE: 2019-07-24

Apparently, on more recent Cygwin installs, you have the following files in your path:

source-highlight.exe
source-highlight-esc.sh
source-highlight-settings.exe

So now you also need to execute the source-highlight-settings.exe that will add the configuration file:
$HOME/.source-highlight/source-highlight.conf.

3
  • 1
    The alias 'lessh' doesn't work for me. Running bash on osx. Sigh.
    – SMBiggs
    Commented Oct 17, 2014 at 17:09
  • That is an entirely different issue, most likely due to OSX using different shell and environment options, which may affect the interpretation of single and double quotes, differently. You can check and set these with shopt -p and set.
    – not2qubit
    Commented Mar 20, 2015 at 0:24
  • 5
    This worked for me on OSX: first I installed with: brew install source-highlight, then I added an alias: alias lessh='LESSOPEN="| src-hilite-lesspipe.sh %s" less -R '
    – mhvelplund
    Commented Aug 23, 2018 at 12:35
58

less doesn't support syntax highlighting.

vim, like all vi clones has a read-only mode called view which you can use to just view files. it supports all features of vim including syntax highlighting.

e.g.

view filename.py

the main difference between view and vi is that view doesn't "lock" the file you're viewing by creating a .swp file.

12
  • if your view command means from radare package , it's not read only Commented Sep 17, 2013 at 6:58
  • 7
    Hmm, my view command doesn't seem to support syntax highlighting. I've tried :syntax enable and :syntax on, but I always get syntax: Not an editor command. My vim supports highlighting, though.
    – Felix
    Commented Sep 17, 2013 at 10:14
  • @felix - do you have another vi like nvi or elvis installed? check your /usr/bin/view - on my debian system, it's a symlink to /etc/alternatives/view which is, in turn, a symlink to /usr/bin/vim.basic.
    – cas
    Commented Sep 17, 2013 at 10:29
  • 1
    Hmm, in my case it's a symlink to /usr/bin/ex, which, curiously, is owned by the vi package.
    – Felix
    Commented Sep 17, 2013 at 10:35
  • 2
    I can confirm what @Felix says, on arch linux view is a symlink to /usr/bin/ex of core/vi package.
    – x-yuri
    Commented May 16, 2015 at 16:51
19

I tend to disagree with Ingo, less can be taught to highlight syntax. Check out this answer on SuperUser. Basically, you have to install GNU's source-highlight (available in all major distro package repos), and then add the following to your .bashrc (or .bash_profile or what have you):

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

However, note that source-highlight is not as powerful as vim's highlighter. Use whatever suits you best.

14

less requires third-party tools to highlight syntax elements, but Vim can be used as a pager, i.e. a replacement for less. There are more advanced plugins, but the basic script actually ships with Vim ($VIMRUNTIME/macros/less.sh). For the full information, see Using vim as a syntax-highlighting pager on the Vim Tips Wiki.

If you're already using Vim, the consistency (and support of almost any file type) makes this worth a try.

4
  • 3
    I'm surprised people doesn't even bother to Google before posting wrong answers...
    – not2qubit
    Commented Jun 29, 2014 at 11:48
  • 1
    @user1147688: If you carefully read Felix's answer, the GNU source-highlight is an external program, and the integration with less is very loose. It's not less that does the highlighting; but Vim indeed has this capability built-in. Commented Jun 29, 2014 at 11:59
  • 4
    For the question "How do I apply syntax highlighting colors according to vim colors for less command?", the answer "By using source-highlight" seems like a more correct answer that "By using vim".
    – Zano
    Commented Mar 10, 2015 at 9:21
  • 3
    +1 but I'm sure what these comments are trying to say -- can't tell if they're even positive or negative ("...google before posting wrong answers"?, "installing xyz is more correct than just using vim"?). This answer is clearly the "most correct answer". Basically the OP didn't know this feature of vim existed, and this answer lets the OP know that it exists. Occam's razor ftw. The answer could only be improved by showing an example, e.g., $ diff file1.c file2.c | /usr/share/vim/vim74/macros/less.sh
    – michael
    Commented Dec 5, 2016 at 2:51
1

After dabbling in some of the answers above, I think the most configuration-free solution is to just install and use vimpager.

It is available on github, homebrew, Arch (older version), AUR (latest), and maybe more.

After install you should be able to immediately use vimpager instead of less at your shell.

To utilise vimpager in other instances where less is used, you can change the PAGER variables your shell config (.bashrc etc.) by adding:

export PAGER=/usr/local/bin/vimpager
export MANPAGER=/usr/local/bin/vimpager

The install paths may (need to?) be different on OSX, but this works fine on Arch linux.

You can then set an alias to always substitute less for vimpager, in the same file:

alias less=$PAGER

(As described at the github readme)

1
  • I'm using vimpager on osx but I don't see syntax highlighting like when I do http localhost:8000|vimpager Commented Jan 10, 2023 at 22:03

You must log in to answer this question.

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