1

In conjunction with https://stackoverflow.com/questions/3131393/remapping-help-in-vim-to-open-in-a-new-tab , I'm looking to also make the shift-K shortcut that opens help in a new tab.

I first tried nmap <S-K> :tab help expand("<cword>")<CR>, but it doesn't actually work - the expand is apparently taken literally as the help tag text, and is not executed.

So, how do I remap <S-K> to get help on the current word in a new tab in Vim?

  • I was going to recommend asking over at Vi and Vim.SE, but is there a reason you asked here instead? – bertieb Aug 23 '15 at 10:09
  • @bertieb No specific reason except the assumption that, with a larger userbase, there might be more probability of getting an answer here. It has turned out my question contained two parts, so I'll now take the unanswered part over to Vi and Vim and try it out there. – sundar Aug 24 '15 at 8:28
  • 1
    I saw you were aware of them so figured it would be something along those lines, cheers :) – bertieb Aug 24 '15 at 9:04
6

I'm not sure what you're trying to do. <s-k>, also known as K, opens the man page for the keyword under the cursor. I assume you want to create a mapping to open the vim help page for the keyword under the cursor. This can be done by this (i'll use <c-k> to not override K):

noremap <c-k> :execute "tab h " . expand("<cword>")<cr>
  • 1
    "<s-k>, also known as K, opens the man page" <- This is actually incorrect: it opens whatever the current keywordprg program is, depending on the filetype; its default is man, but the keywordprg automatically changes to :help when in a vimscript file, and can be set to for eg. perldoc -f for filetype perl, to automatically get perldoc help within Perl files when K is pressed. – sundar Aug 24 '15 at 8:23
  • What I wanted was a way to take this whole thing and just add the feature of opening the output in a new tab. Your execute idea did help me make this work just for vim keywords at least, so I'll accept your answer and split the question about making this portable for any value of keywordprg a separate question (since that looks to be a more complicated requirement). – sundar Aug 24 '15 at 8:25
  • 1
    I see what you are trying to do. You can achieve this with this mapping (i'll use <c-k> again): nnoremap <c-k> :execute 'tabnew <bar> read !' . &keywordprg . ' ' . expand("<cword>")<cr> – madmax1 Aug 24 '15 at 8:36
0

For the sake of anyone stumbling upon this later: I combined the two methods mentioned in @madmax1's answer and comment, to make Vim choose the appropriate method of help automatically depending on the filetype.

function! GetHelpOnCwordInTab()
    if &filetype == "vim"
        execute 'tab help ' . expand("<cword>")
    else
        execute 'tabnew <bar> read ! ' . &keywordprg . expand("<cword>")
    endif 
endfunction
autocmd FileType * nnoremap <C-K> :call GetHelpOnCwordInTab()<CR>

(I had to take @madmax1's suggestion on keymapping too (C-K instead of S-K) because Scriptease has an autocmd overwriting the S-K mapping, and I'm not sure how to override that from within .vimrc.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.