vim as a syntax highlighting capable pager

Just thought I’d share what I learned sometime back.

Do you ever wish that you could “cat” a file and have it displayed on the terminal with syntax highlighting ? Well, “cat” is a tool for concatenating files. It does not know (or care) where it is cat-ing to (for that matter, it does not know or care, where it is cat-ing from either)

OTOH, Pagers (like ‘more’ or ‘less’) are tools to view files on the terminal. That said, vim also behaves somewhat like a pager when invoked using the command ‘view’. However, this does not do syntax highlighting, nor does it take input from stdin (ie: executing “cat /some/file | view” does not do what you expect). So what do we do ?

Well, we use a little a vim trick:

  • Look for the file /usr/share/vim/vim<version>/macros/less.sh
  • create an alias for that script:

    $ alias vless='/usr/share/vim/vim/macros/less.sh'

    (you may also save the alias later in ~/.bashrc if you like the way it works)

  • now use vless as you would use less

    $ vless foo.c
    $ cat /some/file | some_filter | vless

Nice huh ?

  1. Yeah, nice one! However, i had to do it like:

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

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>