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?

5
  • 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, 2021 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. Dec 20, 2021 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, 2021 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 Dec 21, 2021 at 16:11
  • What did you try?
    – romainl
    Jun 13, 2023 at 10:46

1 Answer 1

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.

2
  • 1
    I do not want to open a help on the current window. I want to show it it in another window if it exists. If not, the current window should be split vertically or horizontally. Dec 20, 2021 at 13:16
  • In which of the split? The top, the bottom, left, right? May 14, 2023 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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