Hello. When using the :help command, depending on the current window settings, the help window opens up in different places and in different ways. There does not seem to be a way to make the help open in the current window, which is very inconvenient for me sometimes. About the :help command is said:
If there is a help window open already, use that one . Otherwise, if the current window uses the full width of the screen or is at least 80 characters wide , the help window will appear just above the current window . Otherwise the new window is put at the very top
This causes weird kind of movement. When I open up the help windows I like to have them on the side, so that I can still easily view my code. Suppose this is my window layout:
+-------+-------+
| | B |
| A +-------+
| | C |
+-------+-------+
In the window A is my code, and in window B and C I want to view two different help topics that are related. These are the steps I take:
B, open the help - [A new window gets created above B]B and keep the newly create help window. New layout:+-------+-------+
| | Help1 |
| A +-------+
| | C |
+-------+-------+
C, open the help - [Cursor jumps back to Help1 and opens help there] . (forgot that this would happen)Help1 was changed in the previous step).Help1 and open the second help topic in the new split. New layout:+-------+-------+
| | Help1 |
| +-------+
| A | Help2 |
| +-------+
| | C |
+-------+-------+
C and close it.The situation gets even more complicated if I want have a layout like this:
+-------+-------+-------+
| | | Help1 |
| A | B +-------+
| | | Help2 |
+-------+-------+-------+
Sometimes the window sizes are less the 80 columns wide (for example when using a larger font on small laptops), and then the help windows open up at the very top like so:
+-----------------------+
| HELP1 |
+-------+-------+-------+
| | | C |
| A | B +-------+
| | | D |
+-------+-------+-------+
This makes using the help window very uncomfortable.
Is there a way to force open the help windows in the current window, without it caring whether another help window is already open and whether window is less than 80 column wide? If not, some option that allows that would be very helpful.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.![]()
Is there a way to force open the help windows in the current window, without it caring whether another help window is already open and whether window is less than 80 column wide?
If you're ok with a workaround, then try this:
vim9script # Init {{{1 var grep_output = '' # Command {{{1 com -bar -nargs=? Help CustomHelp(<q-args>, <q-mods>) cnorea <expr> h getcmdtype() == ':' && getcmdpos() == 2 ? 'Help' : 'h' # Interface {{{1 def CustomHelp(atagname: string, mods: string) #{{{2 if empty(atagname) exe 'e ' .. &helpfile :1 return endif var tagname = getcompletion(atagname, 'help')->get(0, '') if empty(tagname) echohl ErrorMsg echom 'E149: Sorry, no help for ' .. atagname echohl NONE return endif grep_output = '' var tagfiles = globpath(&rtp, 'doc/tags', true, true) ->map({_, v -> shellescape(v)}) ->join() var shellcmd = printf("grep -F -H '\t/*%s*' %s", tagname, tagfiles) job_start(['/bin/sh', '-c', shellcmd], { out_cb: GetGrepOutput, exit_cb: function(Jump, [tagname, mods]), mode: 'raw', noblock: true, }) enddef #}}}1 # Core {{{1 def GetGrepOutput(_: channel, data: string) #{{{2 grep_output ..= data enddef def Jump(tagname: string, mods: string, ...l: any) #{{{2 sleep 1m var fname = split(grep_output, '\n') ->get(0, '') ->matchstr('^.\{-}\t\zs\S\{-}\ze\t') if empty(fname) | return | endif fname = globpath(&rtp, 'doc/' .. fname, true, true)->get(0, '') if empty(fname) | return | endif var cmd = { aboveleft: 'aboveleft split', belowright: 'belowright split', botright: 'botright split', leftabove: 'leftabove split', rightbelow: 'rightbelow split', tab: 'tabedit', vertical: 'vertical split', }->get(mods, 'edit') exe cmd .. ' ' .. fname search('\V*' .. tagname .. '*') redraw enddef
It installs a custom :Help command which tries to behave as you described. Write the code in ~/.vim/plugin/CustomHelp.vim. You don't have to type the full name of the command. If you just type h at the start of the command-line followed by a space, an abbreviation should expand h into Help. It supports modifiers like :tab and :vert; although, if you use any of them, the abbreviation won't kick in, and you'll have to type Help in full.
@lacygoill , thank you for your answer. I tried it out but for some reason it does not seem to be working for me. When I type :h it does indeed autocomplete to :Help , but when typing a topic, for example :Help grep, it does not really do anything. Maybe it has something to do with vim9.
I appreciate your answer, but I think this still think the "force open in current window" option should still be there by default.
Maybe it has something to do with vim9.
No, I tested the code, and it works as expected on Ubuntu 16.04 with Vim 8.2.2208. However, it relies on grep(1). Maybe you're on Windows, or maybe your grep(1) is different than mine, and has a different syntax.
I appreciate your answer, but I think this still think the "force open in current window" option should still be there by default.
Understood. I think what you want is a ++curwin option; similar to what :term supports, which you could use like this:
:h ++curwin topic
Maybe you're on Windows
Ah yes, of course, I am, I completely missed that even myself.
I think what you want is a ++curwin option; similar to what :term supports
Yes, that would be nice. If possible, being able to set this option in my vimrc, and forget about it, would be nice as well. For example set curwin=term,help. But that is of course just the cherry on top :)
How about:
edit $VIMRUNTIME/doc/help.txt help {subject}
@brammool, yeah that is fair, but then on each window has to be called two commands, one of which is lengthy to type. Is there a way to automate that? So that help subject automatically calls edit $VIMRUNTIME/doc/help.txt beforehand?
For me, it doesn't seem to work; that is, I still get two windows instead of one, because :h still splits the current window, even when the latter displays a help file. Maybe I'm missing some option though.
Is there a way to automate that?
Try this:
au CmdlineLeave : if getcmdline() =~# '^h\%[elp]\s' | exe 'e ' .. &helpfile | endif
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups "vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_dev/vim/vim/issues/7534/751371195%40github.com.
@lacygoill, you are right, I tested it myself just now, it really opens a new split window instead of using the one that is there. I assumed it would not do that so I did not test it.
Try this:
au CmdlineLeave : if getcmdline() =~# '^h\%[elp]\s' | exe 'e ' .. &helpfile | endif
Thank you!
Anyway, I agree that would be a useful feature. I had a look at the todo list but couldn't find a relevant item in the section dedicated to the help window:
Maybe it will be added to the todo list.
I can't help further, as I only know Vim script. In the meantime, here's an update of the previous script which doesn't rely on grep(1) nor on a job (those were 2 of the 3 possible issues for you):
vim9script com -bar -nargs=? Help exe CustomHelp(<q-args>, <q-mods>) cnorea <expr> h getcmdtype() == ':' && getcmdpos() == 2 ? 'Help' : 'h' var taglines: list<string> def CustomHelp(atagname: string, mods: string): string
if empty(atagname)
exe 'e ' .. &helpfile
cursor(1, 1)
return '' endif var tagname = getcompletion(atagname, 'help')->get(0, '') if empty
(tagname)
return 'h ' .. atagname
endif
var tagpat = '\V*' .. tagname .. '*'
if empty(taglines)
taglines = globpath(&rtp, 'doc/tags', true, true)
->map((_, v) => readfile(v))
->flatten()
endif
var idx = match(taglines, tagpat)
if idx == 0
return 'h ' .. atagname
endif
var help_filename = taglines[idx]
->matchstr('\t\zs\S\+\ze\t')
var help_filepath = globpath(&rtp, 'doc/' .. help_filename, true, true)->get(0, '')
if empty(help_filepath)
return 'h ' .. atagname
endif
exe 'e ' .. help_filepath
search(tagpat)
return ''
enddefHere is the same code in Vim script legacy, in case the script breaks in the future because of a syntax change:
com -bar -nargs=? Help exe s:CustomHelp(<q-args>, <q-mods>) cnorea <expr> h getcmdtype() == ':' && getcmdpos() == 2 ? 'Help' : 'h' fu s:CustomHelp(tagname, mods) abort if empty(a:tagname) exe 'e ' .. &helpfile call cursor(1, 1) return '' endif let tagname = getcompletion(a:tagname, 'help')->get(0, '') if empty(tagname) return 'h ' .. a:tagname endif let tagpat = '\V*' .. tagname .. '*' if !exists('s:taglines') let s:taglines = globpath(&rtp, 'doc/tags', v:true, v:true) \ ->map({_, v -> readfile(v)}) \ ->flatten() endif let idx = match(s:taglines, tagpat) if idx == 0 return 'h ' .. a:tagname endif let help_filename = s:taglines[idx] \ ->matchstr('\t\zs\S\+\ze\t') let help_filepath = globpath(&rtp, 'doc/' .. help_filename, v:true, v:true)->get(0, '') if empty(help_filepath) return 'h ' .. a:tagname endif exe 'e ' .. help_filepath call search(tagpat) return '' endfu
those were 2 of the 3 possible issues for you
There is a third possible issue. The script assumes that a line in a tag file follows this format:
:help helphelp.txt /*:help*
├───┘├─────┘├──────────┘├──┘├──────┘
│ │ │ │ └ tag name
│ │ │ └ tab character
│ │ └ help file where the help tag can be found
│ └ tab character
└ tag name
That's the one I observe on Linux, but I don't know whether this is the same format which is used on Windows. If it's not, the regex on the following line of code might need to be adjusted to correctly extract the basename of the help file where the help tag can be found:
->matchstr('\t\zs\S\+\ze\t')
Thank you very much :)
@lacygoill thank you very much :)
It works only when the help.txt file was opened before:
help
close
edit $VIMRUNTIME/doc/help.txt
help {subject}
You'll have to wrap that in a user command to avoid typing all the commands.
It works only when the help.txt file was opened before:
Can confirm that it works. So, here is an update to the previous autocmd:
au VimEnter * sil h | close
au CmdlineLeave : if getcmdline() =~# '^h\%[elp]\s' | exe 'e ' .. &helpfile | endif
Better with a self-clearing augroup:
augroup HelpCurwin
autocmd!
autocmd VimEnter * silent help | helpclose
autocmd CmdlineLeave : if getcmdline() =~# '^h\%[elp]\s' | execute 'edit ' .. &helpfile | endif
augroup END
Maybe we should document this as a useful tip somewhere at :h tip:
diff --git a/runtime/doc/tips.txt b/runtime/doc/tips.txt index a66de919a..93d992fad 100644 --- a/runtime/doc/tips.txt +++ b/runtime/doc/tips.txt @@ -30,6 +30,7 @@ Executing shell commands in a window |shell-window| Hex editing |hex-editing| Using <> notation in autocommands |autocmd-<>| Highlighting matching parens |match-parens| +Forcing help to open in current window |help-curwin| ============================================================================== Editing C programs *C-editing* @@ -530,4 +531,16 @@ A slightly more advanced version is used in the |matchparen| plugin. autocmd InsertEnter * match none < +============================================================================== +Forcing help to open in current window *help-curwin* + +By default, the help is opened in a split window. If you prefer it opens in +the current window, try these autocmds: +> + augroup HelpCurwin + autocmd! + autocmd VimEnter * silent help | helpclose + autocmd CmdlineLeave : if getcmdline() =~# '^h\%[elp]\>' | execute 'edit ' .. &helpfile | endif + augroup END +< vim:tw=78:ts=8:noet:ft=help:norl:
Ok, I can see that this issue can be worked around with. Feel free to close this issue if there is nothing to add to this.
Closed #7534.
Opps, sorry, leaving it open, so that Bram can include the doc patch.
Reopened #7534.
I do not like the suggestion. Nothing should happen on startup, only when help is actually wanted.
Also, it should be done with a user command, the user must explicitly want to open the help in the current window.
There are many situations where this is not desired, such as when the buffer has changes.
Understood. So here is a new patch, which instead provides a custom :HelpCurwin command:
diff --git a/runtime/doc/tips.txt b/runtime/doc/tips.txt
index a66de919a..97b9ff626 100644
--- a/runtime/doc/tips.txt +++ b/runtime/doc/tips.txt @@ -30,6 +30,7 @@ Executing shell commands in a window |shell-window| Hex editing |hex-editing| Using <> notation in autocommands |autocmd-<>| Highlighting matching parens |match-parens| +Forcing help to open in current window |help-curwin| ============================================================================== Editing C programs *C-editing*
@@ -530,4 +531,27 @@ A slightly more advanced version is used in the |matchparen| plugin.
autocmd InsertEnter * match none
<
+============================================================================== +Forcing help to open in current window *help-curwin* + +By default, the help is opened in a split window. If you prefer it opens in
+the current window, try this custom `:HelpCurwin` command: +> + command -bar -nargs=? 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 +<
+
vim:tw=78:ts=8:noet:ft=help:norl:—
Ah, the custom command should provide a completion for help subjects just like the default :help. Updated patch which uses -complete=help:
diff --git a/runtime/doc/tips.txt b/runtime/doc/tips.txt index a66de919a..97b9ff626 100644 --- a/runtime/doc/tips.txt +++ b/runtime/doc/tips.txt @@ -30,6 +30,7 @@ Executing shell commands in a window |shell-window| Hex editing |hex-editing| Using <> notation in autocommands |autocmd-<>| Highlighting matching parens |match-parens| +Forcing help to open in current window |help-curwin| ============================================================================== Editing C programs *C-editing* @@ -530,4 +531,27 @@ A slightly more advanced version is used in the |matchparen| plugin. autocmd InsertEnter * match none < +============================================================================== +Forcing help to open in current window *help-curwin* + +By default, the help is opened in a split window. If you prefer it opens in +the current window, try this custom `:HelpCurwin` command: +>
+ 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 +< + vim:tw=78:ts=8:noet:ft=help:norl:
—
Adding a link at :h {subject} would make the tip more discoverable:
diff --git a/runtime/doc/helphelp.txt b/runtime/doc/helphelp.txt index c724923e4..1eef35483 100644 --- a/runtime/doc/helphelp.txt +++ b/runtime/doc/helphelp.txt @@ -101,6 +101,9 @@ Help on help files *helphelp* find a tag in a file with the same language as the current file. See |help-translated|. + For how to open the help in the current window, rather + than a new split, see |help-curwin|. + *:helpc* *:helpclose* :helpc[lose] Close one help window, if there is one. Vim will try to restore the window layout (including diff --git a/runtime/doc/tips.txt b/runtime/doc/tips.txt index a66de919a..dfc1df623 100644
--- a/runtime/doc/tips.txt +++ b/runtime/doc/tips.txt @@ -30,6 +30,7 @@ Executing shell commands in a window |shell-window| Hex editing |hex-editing| Using <> notation in autocommands |autocmd-<>| Highlighting matching parens |match-parens| +Forcing help to open in current window |help-curwin| ============================================================================== Editing C programs *C-editing* @@ -530,4 +531,27 @@ A slightly more advanced version is used in the |matchparen| plugin. autocmd InsertEnter * match none < +============================================================================== +Forcing help to open in current window *help-curwin* + +By default, the help is opened in a split window. If you prefer it opens in +the current window, try this custom `:HelpCurwin` command: +>
+ command -bar -nargs=? 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 +< + vim:tw=78:ts=8:noet:ft=help:norl:
—
I'll include it, thanks.
closing then.
Closed #7534.
I'd rather type h and have it not open a split. Makes no sense to me that Ex command has prefixes for splits and the help does'nt.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
The current help behaviour won't be changed. We don't know what is in the current buffer going on, so opening the help in a split window makes sense and has always been like this. You can use the suggested command from the help window if you want a different behaviour. But we won't be changing the existing help window behaviour
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
We don't know what is in the current buffer going on, so opening the help in a split window makes sense and has always been like this
One could say the same for opening files, why can I open a file in a new buffer using "e" then? There are no assumptions in both scenarios:
The user ask for the help
The user ask for a file
It's exactly the same thing.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
Except that the help has always been opened in a new window. This is not going to be changed.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups "vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/vim_dev/vim/vim/issues/7534/2600790417%40github.com.