Home > Linux > Use Vim As A Syntax Highlighting Pager

Use Vim As A Syntax Highlighting 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.

If this article has been helpful, please consider linking to it.

Categories: Linux Tags: , , , , ,

Related Posts

  1. Charles
    July 14th, 2008 at 18:37 | #1

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

  2. July 14th, 2008 at 22:53 | #2

    Also, if you are in less, you can invoke your $EDITOR by hitting ‘v’

  3. July 15th, 2008 at 00:56 | #3

    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.

  4. July 15th, 2008 at 03:05 | #4

    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

  5. July 15th, 2008 at 03:22 | #5

    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 -

  6. July 15th, 2008 at 03:24 | #6

    Ops, got my name wrong in the previous comment :)

    Looks like it was prefilled with the previous posted fields.

  7. Stoffe
    July 15th, 2008 at 05:26 | #7

    Thanks! :)

  8. Myron
    July 15th, 2008 at 11:14 | #8

    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.

  9. July 16th, 2008 at 03:47 | #9

    Thanks.

  10. July 16th, 2008 at 08:51 | #10

    How about vimmanpager ?

  11. July 17th, 2008 at 05:28 | #11

    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

  12. Mark
    August 21st, 2008 at 09:35 | #12

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

  13. September 24th, 2008 at 13:39 | #13

    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

  14. Bit
    March 7th, 2009 at 09:35 | #14

    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”‘

    ;)

  15. April 8th, 2009 at 03:06 | #15

    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?

  16. Ben L
    April 26th, 2009 at 15:35 | #16

    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

  17. skip tari
    February 4th, 2010 at 13:00 | #17

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