Vim syntax highlighting with less

I find that vim's syntax highlighting is incredibly useful in making documents more readable. So much so that I find myself wishing that I had this feature when simply paging through files quickly with 'less'. Turns out the vim folks thought of this too and include a script that allows you to use your finely honed vim environment as a replacement for less.

Buried within your vim install is a bash script called 'less.sh' which will call vim as the pager, and make it act like less, while bringing all the power of vim and its syntax highlighting. Using this is reasonably trivial, requiring only an alias in your ~/.bashrc. Since I have a home directory shared between various versions of RHEL with various versions of vim installed, I added a simple test to find this file before creating my alias.

In my ~/.bashrc I have the following block of code:

VLESS=$(find /usr/share/vim -name 'less.sh')
if [ ! -z $VLESS ]; then
  alias less=$VLESS
fi

This code block will look through /usr/share/vim for the less.sh file, and if it's found, will set that as the alias for less. If it doesn't exist, you get the default less binary as your pager. It doesn't matter what version of RHEL or vim you're running, it'll find the script and allow you to use it. Now you can simply source your .bashrc file to begin using it immediately, or spawn yourself a new bash instance. The standard less keys should work just fine... arrows to move, v to edit, etc.



2 comments:

Post a Comment

Copyright © Bit Integrity