Open vim help for word under cursor

4 Points

Anton Backer Anton Backer

10 years ago

Maps "K" to open vim help for the word under cursor when editing vim files. This already is the system default on Windows, but it needs to be added explicitly on Linux / OS X.

autocmd FileType vim setlocal keywordprg=:help

ReneFroger

ReneFroger 10 years ago

I don't get it. The letter K is standard in Vim, which opens the help on the tag under cusror. So why make a map for it?

Anton Backer

Anton Backer 10 years ago

Hmm, can you show me where in the vim files this mapping is defined?

ReneFroger

ReneFroger 10 years ago

Simply type :help K

Florian Beer

Florian Beer 10 years ago

Kis mapped to keywordprgwich runs 'man' by default. This tip remaps it to VIM's internal help.

rojspencer

rojspencer 10 years ago

Nice one. Very useful when editing vimrc.

ReneFroger

ReneFroger 10 years ago

@Florian: for example, I set the cursor on 'backup'.

I get the help contents about backup with Vim's default help K-key and when I tried the code snippet, I found no difference.

So maybe I'm wrong, but I don't get any idea what the difference here is. Both keys open the Vim help. Can you enlighten me, please?

Florian Beer

Florian Beer 10 years ago

Interesting, when I place the cursor on the word 'backp' and then press K (in normal mode) VIM opens the shell and searches for a manpage for backup. Only the command :h backup yields the result you are talking about.

I am on OS X by the way. Maybe you're on windows, where there is no shell and no manpages and VIM for windows intelligently tries to work around that?

justrajdeep

justrajdeep 10 years ago

hi

you can just do

set keywordprg=":help"

also

ReneFroger

ReneFroger 10 years ago

@Florian. That's true, I'm on Windows. But I don't know if that caused this behaviour, 'cuz the remapping and internal K key are depending on Vim's settings and not Windows, I assume?

Anton Backer

Anton Backer 10 years ago

ReneFroger / Florian: yeah, keywordprg indeed seems to be set to :help by default on Windows. It's hard-coded right into the source, specifically for Windows builds: https://code.google.com/p/vim/source/browse/src/option.c#1657

On most other systems, however, it defaults to man unless overridden by a specific ftplugin (e.g. erlang, git, perl.) ftplugin/vim.vim does not explicitly override it, so this tip is worthless on Windows but useful on Linux / OS X.

justrajdeep: Good call, I'll update the mapping. For historical purposes, my ugly original mapping was autocmd FileType vim nnoremap <buffer> K :execute 'help ' . expand("<cword>")<cr>.)