I’ve talked casually about using Vim as a pager before. However, I’m still surprised to see how many people use Vim regularly and don’t know about this feature.
Here’s a quote straight from vim --help
vim [arguments] - read text from stdin
Admittedly, it’s easy to overlook the hyphen in the explanation.
Why Vim as a Pager?
If you’re using Vim already, there’s nothing else to install.
If you’re using Vim already, it’s already configured the way you like it.
More importanly, Vim detects the kind of file it is being piped and turns on the appropriate syntax highlighting. Why page in black and white? In this case, “less” is definitely less!
Improving the experience
As a pager, you want to use Vim in read-only mode.
some command | vim -R -
What the difference? Vim doesn’t ask you to save the file if you try to quit. Of course, you can still modify and write the file … the -R flag is just a more reasonable pager default.
PAGER variable and ANSI Escape Sequences
You probably don’t want to set the PAGER variable. Vim doesn’t understand ANSI escape sequences. As such, a command like “man vim | vim -R -” won’t show colors; it will show escape sequences.
I haven’t found any quick and simple solution to make Vim show ANSI escape sequences, but it’s pretty easy to strip them out before passing the file to Vim:
man vim | col -b | vim -R -
I use less as PAGER. I use vim in explicit cases.
View
The view command gets installed at the same time as vim. It’s just a symlink to vim. Using view is exactly like typing vim -R.
There’s a certain aesthetic in:
some command | view -
But I find that typing vim -R - is easier on my finger’s muscle memory.
Hi
You mentioned that vim will automatically turn on syntax highlighting depending on the file type being piped, say if i want to page a large java class, and i do
cat LongClass.java | vim -R –
I still see the content in black and white, rather than the coloured syntax as vim LongClass.java.
What am I missing here?
Thanks
Jim
you could use it like this:
cat LongClass.java | vim -R – “+set filetype=java”
@Jim
It depends on how you are doing syntax highlighting.
If you have autobuf rules based on file extension, it won’t recognize the file from stdin.
You need to be using the automatic filetype detection; make sure you have something like:
filetype plugin indent on
syntax enable
in your vimrc.
I don’t have a java file lying around, but I tried piping a python file into ‘vi -‘ and it was highlighted correctly.
Thanks :-)
“Why page in black and white? In this case, “less” is definitely less!”
You can configure less to do syntax highlighting, using enscript and .lessfilter. See e.g.:
http://www.speedblue.org/less_color.php
@bobby is right, you need “filetype plugin indent on” for detection to work.
for java … I’m afraid you’re out of luck.
I went through scripts.vim (which looks inside the file to decide the filetype) and I guess there’s no easy hook to decide it’s java and not something else.
Inside vim, however, you could type “:set ft=java”. This is definitely not optimal, but it depends on what you’re trying to achieve.
If nothing else works, you can always put a vim modeline somewhere in the comment section of the file:
// vim:ft=java:
Hi All,
With filetype plugin indent on, it seems the only file types that didn’t have syntax highlighting are Java based files (*.java, *.groovy..etc), other file types work as described.
Thanks for all your suggestions :-)
Jim
There’s also a vimpager script on the vim.org scripts page:
http://www.vim.org/scripts/script.php?script_id=1723
[…] Using vim as a pager I’ve talked casually about using Vim as a pager before. However, I’m still surprised to see how many people […] […]