0

So I want to be able to automatically open help files in a new tab.

I saw a question on this SE that pretty much solved my problem.

I wanted to make just one change: center the screen.

"That will be easy," I thought....

"Only apply to .txt files...
augroup HelpInTabs
    autocmd!
    autocmd BufEnter  *.txt   call HelpInNewTab()
augroup END

let g:help_in_tabs = 1

"Only apply to help files...
function! HelpInNewTab ()
    if &buftype == 'help' && g:help_in_tabs
        "Convert the help window to a tab...
        execute "normal \<C-W>T"
        " THIS IS MY CHANGE:
        execute "normal zz"
    endif
endfunction

quite unexpectedly, there is no difference. ☁️

4
  • 1
    Do you have zz mapped to anything? Just in case, use normal! zz (I'm pretty sure you don't need the execute statement either. Also, you can just do :tab help <topic> to open a help page in a new tab.
    – mattb
    Commented Sep 15, 2022 at 19:23
  • Yes I use nescroll.nvim which maps zz. But I just tried it with normal! zz and also before with gg to no avail 😕 Commented Sep 16, 2022 at 8:15
  • I prefer to open help in a split window, and possibly on the right (with wide screens there is enough room to keep help and text open at the same time). I created a mapping: map <Leader>ve :vertical botright help<CR>
    – Antonio
    Commented Sep 16, 2022 at 15:59
  • I tried your code. Manually calling HelpInNewTab() gives the desired result. Even running :doautocmd works. But when firing automatically, there is no change, as you mentioned. In addition, I found that after switching to another tab and back, the screen is centered! I think this might be a bug.
    – husB
    Commented Sep 28, 2022 at 16:26

1 Answer 1

1

I tried your code, and indeed, zz has no effect. In fact, reducing the function to contain only normal zz did not center the help window automatically.

A workaround is to use a timer. That is, in your function, replace execute "normal zz" with

call timer_start(0, {-> feedkeys("zz")})

Let's break it down:

  • timer_start creates a timer, that executes a function after some time specified by the first argument, which is...
  • 0, that is, execute the following function immediately
  • {-> feedkeys("zz")}: a lambda expression (anonymous function), which behaves as though the user entered zz.

Basically, after waiting (albeit for 0 seconds), press zz.

For more, see

  • :h timer,
  • :h expr-lambda,
  • :h feedkeys().

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.