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. 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 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 sure you&…

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 yet …

Howto setup a MySQL Connector/J 5.1 for Tomcat on Linux

Again, I'm not switching to Java. :) For clarity, I'm helping one of my online buddies to setup and use Ruby even as I write this. This work was something I had to do for a Rails project which used JSPs and stuff with a MySQL database over JDBC. The application setup was quite interesting calling JSPs to work with a Rails webapp.

Actually the following things are found on the Internet. I cannot remember all the sources I looked at, but one was the MySQLs own documentation and Apache Tomcat documentations. So if this works (which in my case did), credit should not be mine. :)

Here's the setup.
GNU/Linux (in my case CentOS 5, although should work with any Linux distro)Apache Tomcat (5.5.25, should work with Tomcat 5.5 range)Sun JDK (1.6.0_04)MySQL (5.0.22)MySQL Connector/J (5.1)

1. I assume that Java is setup (See my previous post for more details on setting up Java manually), and your MySQL is running on the same host on port 3306. Please replace your actual settings if they ar…