sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #485: Open a new window and read in the man page for the word under the cursor

 tip karma   Rating 15/8, Viewed by 1087 

created:   June 13, 2003 22:25      complexity:   intermediate
author:   munk      as of Vim:   6.0

This short function opens a new window and reads in the Unix man page for the word under the cursor.  To use it add the following to your ~/.vimrc file:

fun! ReadMan()
    " Assign current word under cursor to a script variable:
    let s:man_word = expand('<cword>')

    " Open a new window:
    :exe ":wincmd n"

    " Read in the manpage for man_word (col -b is for formatting):
    :exe ":r!man " . s:man_word . " | col -b"

    " Goto first line...
    :exe ":goto"

    " and delete it:
    :exe ":delete"
endfun

" Map the K key to the ReadMan function:
map K :call ReadMan()<CR>

cf:
:help windows
:help wincmd
etc

The col command may differ on your version of Unix, see col(1) for details.

 rate this tip  Life Changing Helpful Unfulfilling 

<<Console-like fonts for Windows GVim | Search for word under cursor, but don't move. >>

Additional Notes

jez.hancock at munk.nu, June 14, 2003 4:34
Addendum:

"AFTER THIS:
    " and delete it:
    :exe ":delete"

" ADD THIS!:
    " finally set file type to 'man':
    :exe ":set filetype=man"

Which formats the page for man type (make sure to 'setenv TERM xterm' first or similar to see the pwetty colours :)
goulding at princeton.edu, June 14, 2003 13:25
Note that if you're using a recent version of groff, you may have to disable SGR (Ansi escape sequences) in order to view man pages within vim.  If you see a whole lot of garbage on the screen, try adding to your .vimrc the line:

let $GROFF_NO_SGR=1
bstrohhaecker at gmx.de, June 16, 2003 1:27
Also take a look at ":e $VIMRUNTIME/ftplugin/man.vim".
lpenz at terra.com.br, June 16, 2003 9:11
I'd rather put in my ~/.vimrc:
source $VIMRUNTIME/ftplugin/man.vim
nnoremap K :Man <C-R><C-W><CR>

jacob at fugal.net, June 18, 2003 8:44
I had the problem that it would open the new window, but loaded the man page itself  into the old window. Changing:

        :exe ":wincmd n"

to

        :exe ":new"

fixed it for me.
cec at NgrOyphSon.gPsfAc.nMasa.gov, June 24, 2003 14:26
Of course, you could use vimscript#489 to do this, too.  In addition, one gets :Man topic  as well as the use of K to look up the word under the cursor.
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.
Sponsored by Web Concept Group Inc. SourceForge Logo