3

:help in Vim will do a window split if there is no help window.

If there is a single window, this is fine.

But when there is already a split (vertical or horizontal), I would like to see the help in a new buffer in another window rather that adding yet another split.

How can I get that?

Vivian De Smedt
  • 16,336
  • 3
  • 18
  • 37
Igor Bukanov
  • 149
  • 1
  • 1
    Do you perhaps mean in another tab? Help does open a new window (sometimes called a split). Try :tab help. – D. Ben Knoble Dec 20 '21 at 15:07
  • @D.BenKnoble I do not want to show help in another tab. Basically if there is a single window, it should be split as currently. But if there is another window besides the current, I want help to use it even if it does not show help. – Igor Bukanov Dec 20 '21 at 16:16
  • 1
    Ok, sounds like filbranden got it right. I was confused by “But when there is already a split (vertical or horizontal), I would like to see the help in a new buffer in another window rather that adding yet another split.” I think perhaps “would like to see the help in one of the pre-existing windows” is clearer to me. – D. Ben Knoble Dec 20 '21 at 16:18
  • You can use <c-w>T / :windcmd T to move the current window to a new tab. Perhaps you can use winrn('$') to get the window count to accomplish your window-to-tab move – Peter Rincker Dec 21 '21 at 16:11
  • What did you try? – romainl Jun 13 '23 at 10:46

1 Answers1

0

The Vim help actually includes a recipe to define a new command :HelpCurwin to open help in the current window, you can find it under :help curwin.

To define the new command, include this snippet in your vimrc (or a *.vim file under directory ~/.vim/plugin would also work):

command -bar -nargs=? -complete=help HelpCurwin execute s:HelpCurwin(<q-args>)
let s:did_open_help = v:false

function s:HelpCurwin(subject) abort let mods = 'silent noautocmd keepalt' if !s:did_open_help execute mods . ' help' execute mods . ' helpclose' let s:did_open_help = v:true endif if !getcompletion(a:subject, 'help')->empty() execute mods . ' edit ' . &helpfile endif return 'help ' . a:subject endfunction

After you have that set up, you can open help in the current window with:

:HelpCurwin

Or, to get help for a particular topic (for example, the :helpgrep command):

:HelpCurwin :helpgrep

Tab completion will work for the command itself (so :H<Tab> might complete it if you don't have any other user-commands starting with H) and for help topics as well.

filbranden
  • 28,785
  • 3
  • 26
  • 71