Vim logo vim online Vim Book Ad

 Tip #167: Using vim as a man-page viewer under Unix

 tip karma   Rating 51/20, Viewed by 2277 

created:   November 21, 2001 7:29      complexity:   intermediate
author:   Charles E. Campbell, Jr.      as of Vim:   6.0

To use vim as a man-page viewer involves setting an environment variable:

    sh, ksh:  export MANPAGER="col -b | view -c 'set ft=man nomod nolist' -"
    csh    :  setenv MANPAGER "col -b | view -c 'set ft=man nomod nolist' -"

Put one of the above two lines into your <.profile> or <.login> file as
appropriate for your shell.

The man pages will then be displayed with vim called as "view" and will use
the <man.vim> syntax highlighting.  I myself use some additional highlighting
which is enabled by putting the following file into <.vim/after/syntax/man.vim>.
I usually use the <astronaut> colorscheme (also available from this archive);
those who use bright backgrounds may find the colors selected for
manSubSectionStart and manSubSection something they'll want to change:

  ---------------------------------------------------------------------
" DrChip's additional <man.vim> stuff

syn match  manSectionHeading    "^\s\+[0-9]\+\.[0-9.]*\s\+[A-Z].*$" contains=manSectionNumber
syn match  manSectionNumber     "^\s\+[0-9]\+\.[0-9]*"              contained
syn region manDQString          start='[^a-zA-Z"]"[^", )]'lc=1      end='"'         contains=manSQString
syn region manSQString          start="[ \t]'[^', )]"lc=1           end="'"
syn region manSQString          start="^'[^', )]"lc=1               end="'"
syn region manBQString          start="[^a-zA-Z`]`[^`, )]"lc=1      end="[`']"
syn region manBQSQString        start="``[^),']"                    end="''"
syn match  manBulletZone        transparent "^\s\+o\s" contains=manBullet
syn case    match
syn keyword manBullet contained o
syn match   manBullet contained "\[+*]"
syn match   manSubSectionStart  "^\*"   skipwhite nextgroup=manSubSection
syn match   manSubSection       ".*$"   contained

hi link manSectionNumber    Number
hi link manDQString         String
hi link manSQString         String
hi link manBQString         String
hi link manBQSQString       String
hi link manBullet           Special
hi manSubSectionStart       term=NONE cterm=NONE gui=NONE ctermfg=black ctermbg=black guifg=navyblue guibg=navyblue
hi manSubSection            term=underline cterm=underline gui=underline ctermfg=green guifg=green
set ts=8
  ---------------------------------------------------------------------

 rate this tip  Life Changing Helpful Unfulfilling 

<<Mapping caps lock to esc in XWindows | Viewing the actual XPM data in GVIM >>

Additional Notes

Douglas Potts <[email protected]>, November 26, 2001 6:10
As part of the man filetype plugin delivered in Vim 6.0, the user command :Man
is setup to view man pages in a similar fashion.  (On Unix) I've sym-linked the
man.vim filetype plugin from the distribution into the plugin directory.  That
was the :Man command is available whenever you need it.

Hope that was useful.
Alper Ersoy <[email protected]>, December 1, 2001 0:38
I had to use PAGER variable instead of MANPAGER.  Version of the man tool I use is 2.3.10.  People who would like to try this may like to check tip #121 too.
[email protected], January 30, 2002 11:24
Two things:

1. Some versions of sh don't support setting environment variables and exporting on the same line.  In that case, you'd have to do:
      > MANPAGER="col -b | view -c 'set ft=man nomod nolist' -"
      > export MANPAGER

2.  Another comment mentioned that their man doesn't support the MANPAGER env var, so they had to use PAGER.  You probably don't want to do that on a permanent basis, since PAGER is used for many other things besides man.  Instead, you might create a small script in your $HOME/bin that sets it in a subshell, for example (vman.sh):

#!/bin/sh
PAGER="col -b | view -c 'set ft=man nomod nolist' - "
export PAGER
man $*

and then do 'vman ls' or whatever.  Replace PAGER with MANPAGER if your man supports it and you want to use the script.

3. You might want to add ignorecase to the list of settings passed to vim (view) to more closely match the searching of most man pagers (less and more).

Ok, I lied - 3 things.
supergrass@gmx.net, February 9, 2002 3:36
even simpler way

what I did was:

1) renamed my less to less.orig
2) edited the less.sh script :

if test $# = 0; then
  vim -c 'so $VIMRUNTIME/macros/less.vim' -
elif test $* = "-is"; then
    less.orig $*
else
  vim -c 'so $VIMRUNTIME/macros/less.vim' $*
fi


3) symlinked $VIMRUNTIME/macros/less.sh to /usb/bin/less


so as you can see when the man is called it just passes the parameters to less.orig

simple, fast works

have phun :)
[email protected], May 16, 2002 20:00
On my Red Hat 7.2 system, view invokes vi (vim in vi compatibility mode), so extra vim features such as syntax highlighting are not available.

To use vim instead of vi for the pager, either manipulate the /bin/view link, or set substitute "vim -R" for "view".
[email protected], August 14, 2002 9:00
Replace view with less.sh of vim will maintain the same keystrokes as less. The following set the MANPAGER for sh, ksh and bash:

export MANPAGER="col -b | /usr/share/vim/vim61/macros/less.sh -c 'set ft=man nomod nolist' -"



[email protected], October 3, 2002 19:19
The original tip works great for me (export MANPAGER="col -b | view -c 'set ft=man nomod nolist' -"), however, if I then try ':Man some-man-page' inside vim after executing man from the shell, I get the following in the window:

Vim: Warning: Input is not from a terminal
^M
Error executing formatting or display command.
System command (cd /usr/man && (echo ".pl 11i"; /bin/gunzip -c '/usr/man/man3/puts.3.gz') | /usr/bin/gtbl | /usr/bin/nroff -S -mandoc | col -b | view -c 'set ft=man nomod nolist' -) exited with status 256.
No manual entry for fputc
?47h?1hmH2J24;1H"-stdin-" [RO] 64L, 1791C1;24rmH2J24;1H?1l2J?47lVim: Error reading input, exiting...
Vim: Finished.
24;1HVim: Reading from stdin...

[Somewhat paradoxically, this occurs only if the man page *does* exist, teehee; if it doesn't, I just get a line telling me 'Cannot find a 'foo'.' in the command-line area.]

I assume it's because :Man doesn't like vim/view being in the MANPAGER, so my current messy workaround is 'let $MANPAGER="/usr/bin/less"' in ~/.vimrc; does anyone have a better solution?  Being able to use K to jump around the manpages is the major reason why I want to use vim as the manpager ;)
Anonymous, November 7, 2002 22:41
I'm having the same problem as ajw. Seems that it's either ":Man", or vim as pager for man from the command line. Making sure that MANPAGER is only set for interactive shells, and then doing "let $MANPAGER='/usr/bin/less'" in .vimrc does help some, as ajw suggests, but it means that you're stuck with less once you've jumped out of the first man page via K. Anybody know how to make vim as MANPAGER and ":Man" from man.vim play nice together?
[email protected], November 27, 2002 6:56
There's now a while-you're-in-vim solution -- check out
vimscript#489 for <Manpageview.vim> for a new :Man command
and support for the K normal-mode command.
Anonymous, December 18, 2002 3:04
Great!
[email protected], December 27, 2002 7:25
Using MANPAGER doesn't work so well with vimscript#489;
instead, I now use a Korn shell function.  Similar
functions could be developed for other shells (bash, zsh),
I'm sure:

# man:
function man
{
/usr/bin/man $* | col -b | /usr/local/bin/view -c 'set ft=man nomod nolist' -
}
jhf.remmelzwaal at europe.com, January 2, 2003 3:06
Reading man pages on NT, Win2000 etc.

Windows NT, etc has remote shell client support. This can be used to read the man pages on a unix system. I changed script vimscript#489 in the following way:
-----------------------------------------------
*** manpageview.vim~ Thu Jan  2 11:42:14 2003
--- manpageview.vim Thu Jan  2 11:44:48 2003
***************
*** 87,94 ****
    enew!
    set mod
  "  call Decho("manpagebook<".manpagebook."> topic<".manpagetopic.">")
!   exe "r!man ".manpagebook." ".manpagetopic
!   %!col -b
    setlocal ft=man nomod nolist
    set nolz
  endfunction
--- 87,98 ----
    enew!
    set mod
  "  call Decho("manpagebook<".manpagebook."> topic<".manpagetopic.">")
!   if has('win32')
!    exe "r!rsh g:manpageview_server -l g:manpageview_user man ".manpagebook." ".manpagetopic." | col -b"
!    1
!   else
!    exe "r!man ".manpagebook." ".manpagetopic
!    %!col -b
!   endif
    setlocal ft=man nomod nolist
    set nolz
  endfunction
-----------------------------------------------
Set your unix system (g:manpageview_server) and user account (g:manpageview_user) in your _vimrc.
Set your NT system name and user account in your ~/.rhosts on your unix system.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo