I do not understand vim script very well, just enough to tinker with my vimrc. So I am looking for someone's help to understand what can be done here.
I use this HelpCurWin command from the vim help page 'tips.txt', tag 'help-curwin'. The command is designed to open a given help page in the current window instead of opening a new split. This part works fine. Here is the code:
" Command to open help page in current window.
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
Here is a slightly diferent version from the nvim documentation (I tried both):
" Command to open help page in current window.
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
" Fix from nvim documentation.
if !empty(getcompletion(a:subject, 'help'))
execute mods .. ' edit ' .. &helpfile
endif
return 'help ' .. a:subject
endfunction
As I was saying the main goal of opening the help page in the current window is achieved, but, there are 3 side effects I noticed:
- When I open a specific help page, the default 'help.txt' page will automatically be added to the buffer list,
- The 'help.txt' page will not be added to the list as an "unlisted" buffer which clutters the tabline, and the buffer list,
- Even if I issue
:HelpCurwinwith no page specified, the 'help.txt' page will be listed as a regular buffer and not get syntax highlighting (even though the filetype is correctly set).
Those are not critical issues, but it is bugging me, and I would be grateful if anyone could help me fix it, or, if impossible, explain me why.
EDIT: I forgot to specify that I am personally using this function mostly from vertically split windows, so I am looking to improve it primarily for that use case.