Skip to main content

Syntax Highlighting Pager: Vim

Pagers

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 less.sh. 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/less.sh
Eg: in your .bashrc specify,
alias vess='/usr/share/vim/vim72/macros/less.sh'

2.
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.

Comments

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

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

    ReplyDelete
  3. Anonymous9:00 PM

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

    ReplyDelete
  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. :)

    ReplyDelete
  5. Anonymous4:54 PM

    Just what I needed :D

    ReplyDelete

Post a Comment

Popular posts from this blog

Howto Migrate from Thunderbird to Evolution

I know some of you are asking why , rather than how , regarding migration from Mozilla Thunderbird to Evolution. Maybe that's why there are lot of Evolution to Thunderbird migration guides, but not many vice-versa. Fear not, here is a guide, to assist who dare to migrate from Thunderbird to Evolution. The techniques described here are tested with the newer versions of both the software, namely Thunderbird 2.0.0.4 and Evolution 2.10.2. On higher versions also this should work without an issue. I think Mozilla people are doing a wonderful job with both Firefox and Thunderbird . From my point of view Firefox is the best general purpose web browser around. It beats most proprietary browser in speed, stability, security, modularity, etc. (and don't start commenting the so and so browsers are greater or so and so is cool too. I know they may be, Fx is simply my choice. This also applies to any comparisons with Evolution too :) However Fxs' counterpart in e mail business, is not y...

Howto Install Docky on Fedora

If you know me personally, then you know that I'm a big fan of GNOME Do . As a keyboard savvy person I use Do extensively. Do is an application launcher similar to the Mac app Quicksilver. However the GNOME Do team has been putting a lot of research and development into it from the initiation. Result: probably the best application launcher out there for any platform. Some months ago, Do included an interesting theme called Docky which made the launcher acts as a dock (a la Mac, Avant, Cairo Dock, etc.). With the integration of GNOME Do, there's no need to say that Docky was super cool. And it started gaining features in a high speed. Ultimately Docky was getting so developed that it became a separate project. Installing GNOME Do on a Fedora system is as easy as: $ sudo yum install gnome-do There are some packages with the names starting from gnome-do-plugins*. With the addition of these GNOME Do can truly enhance your desktop experience. Give it a fair try, I'm pretty...

Howto Setup a Subversion (svn) Repository for a Rails Project + Bonus

Setting up a Subversion (svn) repository is something development teams have to do fairly regularly, not that I want to use Subversion. :) If you listen to me, go use Git . Subversion is undoubtedly very good. But after using Git for about a year, you can't simply get me to switch back. Git is that good. :) I've written about Git before . In cases where you can't use Git (or you feel too castrated by TortoiseSVN, pardon me for the pun) you can use Subversion. In this post I'll go through the steps you have to follow to get a basic Subversion setup up and running on a CentOS 5 Linux host. However I think you should be able to use this on other Linux distros too. There's more than one way to host a Subversion repo. I'm going to stick with one way involving WebDAV. Don't mind the buzz word. It's the most common usage for this purpose. If you want a repository where you want to checkout and commit remotely, this is an easy way of getting it done. In addition...