Thursday, July 23, 2009

Syntax Highlighting Pager: Vim


If you use a Linux/Unix system using a pager should be quite familiar to you. A pager in the Unix sense is a program which lets you view pages of contents. Examples for pagers are the Unix commands "less" and "more". It's likely that your man page viewer is also using one of these.

The program less is perhaps the most popular of these, with more features than "more". These pagers are nice to use use and come very handy in day to day Linux/Unix operations. When you just need to view a file (instead of opening it for editing) it's quite natural to use less. If it's a file with a few lines we'd usually use cat, but less is more convenient on longer files.

One irritating thing about less is, there is no syntax highlighting support there by default. You can try to use an external program like "highlight" for the highlighting part and then pipe it to less. (Eg: $ highlight --ansi --force my_file | less -R) But that's tedious. If the filetype isn't supported by "highlight" you need to add them manually.

Enter Vim

If you have painstakingly tuned a beautiful Vim environment like I did, you might be wondering if there's a way to use Vim as a pager. In fact you can.

The good folks at the Vim project ships a nice configuration file and a shell script just in case you love Vim so much that you want it to be your pager too. It's usually located under the macros directory in your Vim installation directory. For example in my case it's: /usr/share/vim/vim72/macros/

The file we need is named It in turn uses a file named less.vim. Which means you can edit the less.vim file in order to introduce your custom changes, if you need any. In that case it's better to copy the two files to somewhere else and do it. That way you leave the original files untouched.

However, if you simply just need to use Vim as a viewer we can do it easily.

1. Create a bash alias to /usr/share/vim/vim72/macros/
Eg: in your .bashrc specify,
alias vess='/usr/share/vim/vim72/macros/'

Next time you log in the alias you created will be ready to use. Or you can just reload the .bashrc again by running
Eg: $ source ~/.bashrc

Now you are good to use vess instead of less. You can use any name you instead of vless as long as it doesn't conflict with existing commands, aliases, etc.

You can always create a link named less so that your custom configuration is used instead of the original less. But I'd rather keep it as it is and use my new "vess" whenever I need a pager.

The final result acts like less. You can traverse with arrow keys, exit with pressing "q" and do on. If you need to edit the file you are viewing you can press "v" to start editing.


  1. Neat. I'm never using less again :)

  2. Yeah. :) That's exactly what I felt too.

  3. Many thanks for this. I had started exploring a route involving pygments 'pygmentize' command, but I much prefer this!

  4. Glad you found it useful. I found this little but convenient script while browsing the contents of the Vim directory. So all I did was to point this in the blogpost. :)

  5. Anonymous4:54 PM

    Just what I needed :D