How can I open a help file in Vim on a new buffer in an existing window?
Asked Answered
U

9

23

I often take a look at help files in Vim, but sometimes I want to read one in full screen. Since the :help command opens it in a new window, and closing the old window, if it was the only one besides of the help file, for some reason closes Vim, the only way I found of doing this was opening the help file, and then reopening it in a new tab.

I wondered, is there any way to make the :help command (or another command) to open a help file in the same window, but a new buffer?

Unwrap answered 29/6, 2010 at 2:33 Comment(0)
C
30

You might be looking for :only or CTRL-W o (the same command). This makes the current window the only one on the screen. All other windows are closed.

You can also vertically split the help window with:

:vert help {subject}

BTW, :help actually does open in a new buffer, it's just "unlisted". To list all buffers, including the unlisted ones:

:buffers!
Chilt answered 29/6, 2010 at 3:47 Comment(0)
P
23

If I understand the question correctly, all you need to do is to chain the help command call with the only command:

:help <subject> | only
Pennate answered 5/2, 2011 at 9:13 Comment(1)
is there a way to make it the default behavior, so that one does not always have to "|only" itReverberator
W
11

The :help will usually open a new window unless the active window's buffer buftype is already help. So to truly reuse a window you must open a new empty buffer in that window with :enew, change the buftype with :set buftype=help and then issue the :help <whatever>.

For convenience you could define a command to do that in your .vimrc:

command! -nargs=1 -complete=help H :enew | :set buftype=help | :h <args>

And then use :H {subject} from any window.

Using this method you truly reuse the window and that allows you to use C-^ to go to the alternate for example. It will also respect your window layout (split windows, etc) unlike the other answers.

Whitelaw answered 17/10, 2014 at 18:56 Comment(1)
In order for C-^ to work you need to add keepalt before :h <args like this: command! -nargs=1 -complete=help H :enew | :set buftype=help | :keepalt h <args>Yama
A
8

You can open a new tab for help with :tab help. This will give you a full screen help. Also look at :help :tab.

Archipenko answered 31/10, 2012 at 8:1 Comment(1)
Another option is as after-thought, open current window in new tab, Ctrl-W T.Nada
C
3

You can use :help to open the help window, then Ctrl+W_ to make that window full screen (mostly, see the winminheight option).

Cubature answered 29/6, 2010 at 2:39 Comment(1)
Wow, I had no idea that you could set winminheight=0 for hiding a Window! While its a great trick, there will still be a little bar with the title of the old window, thats why my question was about opening a new buffer :PPensionary
D
2

To open full size new tab with your desired topic:

:tab help {subject}
:tab h {subject}

Subject is any valid :help argument.

To split the current window:

:vert help {subject}
:vert h {subject}
Dogberry answered 3/2, 2017 at 17:53 Comment(0)
P
2

A more general variant of @Shamaoke's answer is to open the main help menu in a full window.

:help | only
Petite answered 16/6, 2020 at 15:32 Comment(2)
10 years after posting this question I'm still learning from its answers. Thanks!Pensionary
Cool! This was a new thing for me too and really useful. :-)Petite
B
1

I wrote a custom command using capital H like so (works exactly like :h except that it uses the whole window):

command! -nargs=1 -complete=help H call HelpFullScreen( <f-args> )

function! HelpFullScreen( topic )
    exe "h " . a:topic
    wincmd j
    try
        clo
    catch /^Vim(\a\+):E444:/ " can't close last window
    endtry
endfunction

Works like a charm!

Bautram answered 5/6, 2013 at 16:41 Comment(0)
L
0

To make :help | only the default behavior, you can use autocmd:

autocmd FileType help wincmd o

or in lua:

vim.api.nvim_create_autocmd(
    "FileType", {
        pattern = {"help"},
        command = "wincmd o",
    }
)
Lind answered 17/7, 2023 at 5:11 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.