31

While working with some Vim scripts, I found myself typing

:help {subject}

quite a bit. I want CTRL+] (jump to definition of keyword under cursor) functionality, but instead of running :tag {ident} I want it to do :help {subject}, where {subject} is the word under the cursor.

jdhao
  • 24,001
  • 18
  • 134
  • 273
Doug Richardson
  • 10,483
  • 6
  • 51
  • 77

3 Answers3

42

Just press K. If you have set a global 'keywordprg', you need to unset it (or set it to the special :help value) in ~/.vim/after/ftplugin/vim.vim:

:setlocal keywordprg=:help
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • 1
    This works like a champ and still keeps K for man page lookups working elsewhere. Thanks. – Doug Richardson Apr 07 '13 at 20:43
  • @DougRichardson and IngoKarkat I haven't been able to put this in modeline for my `.vimrc`. Should I take the `aucmd` approach? Otherwise, I might just use `K` with ZyX's solution. – Brady Trainor Jul 18 '14 at 00:33
  • I don't know if modelines work with this method. I just tried using `" vim: keywordprg=:help` and got a message `E520: Not allowed in a modeline: keywordprg=`. – Doug Richardson Jul 18 '14 at 00:52
  • 4
    I now have `au FileType vim setl keywordprg=:help`. Oh... that must be what the `ftplugin/vim.vim` is for. Oops. – Brady Trainor Jul 19 '14 at 18:02
  • Either one is fine. I prefer the au method myself. Then it's in my .vimrc, which is in the cloud. – xizdaqrian Dec 15 '14 at 01:00
  • To do it with a modeline @DougRichardson, set it in your buffer first. Then in command mode (:), type something like... let mode_line = printf( "Keywordprg: %s", &keywordprg )... hit enter – xizdaqrian Dec 15 '14 at 01:11
  • ... (sorry) then :call append(line("."), ml)... hit enter. This will paste that setting on the current line. To be a true modeline, it needs a little more. Have a look here: http://vim.wikia.com/wiki/Modeline_magic – xizdaqrian Dec 15 '14 at 01:13
  • 1
    If you just need the window as a reference (or at least most time), you can save yourself a couple of strokes adding `au FileType vim nnoremap K K`. – Pablo Olmos de Aguilera C. Feb 22 '16 at 02:03
12

The simplest solution is

nnoremap K :help <C-r><C-w><CR>
ZyX
  • 52,536
  • 7
  • 114
  • 135
  • 4
    Well, almost. Looks like that hi-jacks man page lookups. Is there anyway to get both? – Doug Richardson Apr 07 '13 at 20:29
  • 1
    Yes... map to the above, and leave alone. Then you get both. If you really want them on the same key, then use an autogroup and autocommands to change the mapping based on filetype. If anyone needs to see that, just ask and I will post something. – xizdaqrian Dec 15 '14 at 00:59
  • @xizdaqrian One does not need to bother with autocommands and changing mapping: just define `` mapping which depending on some condition will either use built-in `K` or `:h`: `nnoremap K (&filetype is# 'vim' ? (':help ' . fnameescape(expand('')) . "\n") : 'K')`. – ZyX Dec 18 '14 at 21:42
-2

To check the documentation of the keyword under your cursor, you can press Ctrl+] to go to its documentation.

If you have enable mouse support in nvim with the following options:

set mouse=a

you can double click the keyword to go to its documentation.

By the way, to go back to previous position in the help file, press Ctrl+O or Ctrl+T.

The above is also true for Neovim.

References

jdhao
  • 24,001
  • 18
  • 134
  • 273