Use Vim As A Pager
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.
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
A very usefull tip. My unique problem was my dark background, the highlight is dark too and it is difficult to read. Solution:
alias vless=’vim -u /usr/share/vim/vim71/macros/less.vim “+set background=dark”‘
😉
Hi,
its a very useful trick, but I need to go a step further. I use “watch” very often, but the above script stops vim without return to shell if the content is shorter then the page height. How to “release” vim and come back to command prompt?
the vim runtime directory has a wrapper script to do this in a more portable way, so it doesn't matter where your macro directory lives (e.g. if you work on alot of different boxes with various versions of vim) have a look at less.sh in your $VIMRUNTIME/macros director
add -n as paraeter to vim to avoid getting the .swp error if file is being edited somewhere else.
Thanks, this is very useful (I use it a lot for inspecting Mercurial diff output) – much appreciated!
Thanks, nice tip!
As of today many distrose ship Vim 7.2 (e.g. Ubuntu) thus the macro is in:
/usr/share/vim/vim72/macros/less.vim
So that the .bashrc file should be:
alias vless=’vim -u /usr/share/vim/vim72/macros/less.vim’
In general one might check for the correct path doing a:
locate less.vim
thanks a lot for that snippet, just added to my bashrc!
I fixed a few problems with that macro file and released it:
http://huyz.us/2011/a-less-like-pager-with-color-syntax-highlighting/
@Fabian Neumann
If you use the link like in the post of Fabian Neumann (the real Fabian Neumann :), you don’t need to put the – for reading from the stdin.
Very nice! I’m using a script to peek at elements from a large XML file with sed and added vim on the pipeline for syntax coloring but less.vim did not work for stdin. But if you know the file type you fix that by appending “+set filetype=xml” like this:
sed large.xml -n -e “/<$TAG name=\"$NAME\"/,//p” | vim -u /usr/share/vim/vim73/macros/less.vim – “+set filetype=xml”
(Tested on Fedora, must be the same on other distros, except for the path to less.vim)