Open a window with the man page for the word under the cursor
From Vim Tips Wiki
created 2003 · complexity intermediate · author munk · version 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:
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>
 Open a Page From a Particular Section in a New Terminal
You might like to be able to hit a key to launch a man page from a certain man section for the word under the cursor:
command -nargs=0 -range ManInGnomeTerm !gnome-terminal --execute bash -i -c 'man $(if [ $(( <line2> - <line1> + 1 )) -ne 1 ]; then echo $(( <line2> - <line1> + 1 )); fi) <cword> || (echo "man lookup failed" 1>&2 && read)' nnoremap <silent> K :ManInGnomeTerm<CR><CR>
This will look up the word under the cursor in a new gnome-terminal. If you press a number key first, it looks only in that manual section (unless the number is 1, in which case you don't need it anyway...). There is one small bug when you try to look in a particular section while near the end of the file, as this results in an Invalid Range error. Just add a few blank lines at the bottom to work around this issue.
The col command may differ on your version of Unix, see col(1) for details.
" 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 :)
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:
Also take a look at ":e $VIMRUNTIME/ftplugin/man.vim".
I'd rather put in my ~/.vimrc:
source $VIMRUNTIME/ftplugin/man.vim nnoremap K :Man <C-R><C-W><CR>
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"
fixed it for me.
Of course, you could use script#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.
This also works:
source $VIMRUNTIME/ftplugin/man.vim nmap K :Man <cword><CR>