Jul
14
Use Vim As A Syntax Highlighting Pager
Filed Under Linux
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 site has been useful, please consider participating in the Fundraiser.
Other Points of Interest
Comments
13 Responses to “Use Vim As A Syntax Highlighting Pager”
Leave a Comment
Great tip! It’s just been added to my aliases.
Also, if you are in less, you can invoke your $EDITOR by hitting ‘v’
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.
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
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 -
Ops, got my name wrong in the previous comment
Looks like it was prefilled with the previous posted fields.
Thanks!
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.
Thanks.
How about vimmanpager ?
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
Uh, on most distros, there is already a “view” hard link to vim, so “view” does the same as your tip.
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