Use Vim As A Syntax Highlighting Pager

By | 2008/07/14

Use Vim As A Pager

It has been some time since I’ve done a Vim Tip of the Week, but I came across something today that I thought I would share.  This tip will allow you to use Vim, with all its syntax highlighting glory, as a pager (similar to less or more).

If you use cat or less or more regularly to quickly view files, but you’d like to keep the same syntax highlighting that you’d get in Vim you can use an included config that makes Vim act as a pager.  Setup the following within your .bashrc file, or wherever you keep your shell aliases:

alias vless='vim -u /usr/share/vim/vim71/macros/less.vim'

You’ll then need to re-read that file, which can be done using:

. .bashrc

At this point you can use vless to view a file, which will use the beloved syntax highlighting.  Normal pager shortcuts should work.  q to quit, / to search, pg-up, pg-dn, etc.

23 thoughts on “Use Vim As A Syntax Highlighting Pager

  1. Charles

    Great tip! It’s just been added to my aliases.

  2. Onkar

    Great tip. I am now going to share it with my colleagues. 🙂

    By the way, you should recommend adding aliases to ~/.bash_aliases and uncomment the part in ~/.bashrc that includes the aliases file.

  3. Fabian Neumann

    The is also a script in vims package (at least in Ubuntu) to start it in less mode. So, what I did is:

    ln -s /usr/share/vim/vim71/macros/less.sh ~/bin/vless

  4. Fabian Neumann

    Thanks for the tip just added it to my aliases.

    If someone is looking for a way to use it with pipes just add – at the end to read from stdin instead of a filename.

    example

    svn diff | vless –

  5. Johan Nilsson

    Ops, got my name wrong in the previous comment 🙂

    Looks like it was prefilled with the previous posted fields.

  6. Myron

    Looks like you have to have the full vim package installed to use it, otherwise the macros directory does not exist. I noticed I had vim-tiny installed by default.

  7. http://mytechieself.blogspot.com/

    hmm, interesting. For those of us who continue to use Vim on Windows (Gvim) and have chosen to install the command-line version during the install process, the following command in the batch file:

    vim -u %VIM%\macros\less.vim %%1

  8. Mark

    Uh, on most distros, there is already a “view” hard link to vim, so “view” does the same as your tip.

  9. Dr Small

    Thanks for the tip. It wasn’t in the same location on ArchLinux as it was on Ubuntu, but I found it here:

    /usr/share/vim/macros/less.vim

  10. Bit

    A very usefull tip. My unique problem was my dark background, the highlight is dark too and it is difficult to read. Solution:

    alias vless=’vim -u /usr/share/vim/vim71/macros/less.vim “+set background=dark”‘

    😉

  11. Artur

    Hi,

    its a very useful trick, but I need to go a step further. I use “watch” very often, but the above script stops vim without return to shell if the content is shorter then the page height. How to “release” vim and come back to command prompt?

  12. Ben L

    the vim runtime directory has a wrapper script to do this in a more portable way, so it doesn't matter where your macro directory lives (e.g. if you work on alot of different boxes with various versions of vim) have a look at less.sh in your $VIMRUNTIME/macros director

  13. skip tari

    add -n as paraeter to vim to avoid getting the .swp error if file is being edited somewhere else.

  14. Rink

    Thanks, this is very useful (I use it a lot for inspecting Mercurial diff output) – much appreciated!

  15. Lorenzo

    Thanks, nice tip!
    As of today many distrose ship Vim 7.2 (e.g. Ubuntu) thus the macro is in:
    /usr/share/vim/vim72/macros/less.vim

    So that the .bashrc file should be:
    alias vless=’vim -u /usr/share/vim/vim72/macros/less.vim’

    In general one might check for the correct path doing a:
    locate less.vim

  16. Trianam

    @Fabian Neumann
    If you use the link like in the post of Fabian Neumann (the real Fabian Neumann :), you don’t need to put the – for reading from the stdin.

  17. Liviu Vasut

    Very nice! I’m using a script to peek at elements from a large XML file with sed and added vim on the pipeline for syntax coloring but less.vim did not work for stdin. But if you know the file type you fix that by appending “+set filetype=xml” like this:

    sed large.xml -n -e “/<$TAG name=\"$NAME\"/,//p” | vim -u /usr/share/vim/vim73/macros/less.vim – “+set filetype=xml”

    (Tested on Fedora, must be the same on other distros, except for the path to less.vim)

Comments are closed.