Opening Vim help in a vertical split window

Is there a way to get Vim help to open in a vertical split pane rather than a horizontal one?

36282 次浏览

:vertical (vert) works:

:vert help

You can also control whether the window splits on the left/top or the right/bottom with ABC0 (to) and ABC2 (bo). For example, to open help in the right window of a vertical split:

:vert bo help

This command should do it:

:vert help

To make help files always open in a vertical split on the right, put this in your vimrc:

augroup helpfiles
au!
au BufRead,BufEnter */doc/* wincmd L
augroup END

This will have the side effect of having anything with "doc" in its path open in a vertical split, but that may not be a problem for you. It isn't for me. If you would rather it open in a left vertical split, or anything else for that matter, you can change wincmd L. You can learn more about it with :he wincmd

Put this in your .vimrc:

command -nargs=* -complete=help Help vertical belowright help <args>

Now you can open a vertical help with the :Help command (notice that the first-letter is uppercase)

No need to remap any commands or introduce weird aliases like :Help. Here is the solution. Create ~/.vim/after/ftplugin/help.vim where you can override any Vim settings particularly for help and add the following line there:

autocmd BufWinEnter <buffer> wincmd L

This hook will ensure that any help file is opened in vertical split. Furthermore, it does not have a side effect described in Sean's answer. Personally, this is perfect solution for me so far.

Hope this helps. Best of luck.

As an alternative to Haroogan and Sean's answers you can use the FileType event for the autocommand like this:

autocmd FileType help wincmd L

Although this will change the position of any help window as well as moving the window after manually placing it if the file you are looking at changes. But I believe that this is a problem with any solution.

This moves the help window once. So you can freely move it around after the window is created.

if has('autocmd')
function! ILikeHelpToTheRight()
if !exists('w:help_is_moved') || w:help_is_moved != "right"
wincmd L
let w:help_is_moved = "right"
endif
endfunction


augroup HelpPages
autocmd FileType help nested call ILikeHelpToTheRight()
augroup END
endif

The function, ILikeHelpToTheRight() will only run wincmd L once per window (it's what the w: prefix is for).

This is then called whenever a "help" file is opened. This doesn't have the side-effects of EdJoJob's solution.

Put the following in your ~/.vim/ftplugin/help.vim

wincmd L

Can't get simpler than this :)

This is meant to add to @m42's answer, but I don't have 50 rep yet here on SO proper to add to the comments.

Add nnoremap <C-H> :vert bo help to .vimrc

Now pressing Ctrl-H in Normal mode will jump into Command mode, prefixed to open help in a vertically split window to the right. Include a trailing space after help·<-- at the end of the config line for best results.

This mapping allows you to still use :help \ :h to open a horizontally split window or cycle through your previous help command history without the prompt auto-expanding.

Dynamically open help windows at the top if there's more than one window in current tab, or on the right, if there's only one window:

if winnr('$') > 2
wincmd K
else
wincmd L
endif

You'll need to place this in ftplugin/help.vim or use it with an autocmd, e.g.:

augroup my_filetype_settings
autocmd!
autocmd FileType help if winnr('$') > 2 | wincmd K | else | wincmd L | endif
augroup END

I settled on the following:

" Open help in a vertical split or a new tab.
augroup my_help
" Remove current group to avoid double runs
autocmd!
" If a help buffer is opened then try to move it to the right. If now it
" doesn't fit help text (78 chars) then move it to a new tab.
autocmd BufEnter * if &filetype == 'help' | wincmd L | if winwidth(0) < 78 | wincmd T | endif | endif
augroup END

It works for me in both vim and neovim. FileType help doesn't work in neovim if I reopen help because the help buffer remains hidden in neovim while vim seems to unload it.

This is what I use, taken from docwhat's answer. You can change values to your liking.

function! MoveWindowToRightOrNewTab()
if winwidth(0) < 165
wincmd T
else
wincmd L
vert resize 85
endif
endfunction


" Open help in a vertical split or a new tab.
augroup HelpWindowOnRight
" Remove current group to avoid double runs
autocmd!
" If a help buffer is opened then try to move it to the right. If now it
" doesn't fit help text (80 chars) then move it to a new tab.
autocmd FileType help call MoveWindowToRightOrNewTab()
augroup END