1

Background: neovim with tagbar plugin installed

Goal: when starting nvim, if current window size is larger than 100, open tagbar and set auto-open for new buffers.

Currently I use the settings below to achieve auto-open for supported files in new buffers:

autocmd VimEnter * nested :call tagbar#autoopen(1)

I want the auto-open be controlled by window width because sometimes I use a narrow terminal split by iTerms or tmux, tagbar is not suitable to open in this situation.

I tried the following but it didn't work:

" if I add `echo winwidth('%') here, it will show 80, not the current terminal width
if winwidth('%') > 100
    autocmd VimEnter * nested :call tagbar#autoopen(1)
endif

It seems that winwidth('%') returns number of columns when nvimrc is executed (I didn't overwrite columns, the default value is 80), so the if is always false and the tagbar never auto-opens. But when I execute :echo winwidth('%') after nvim is opened, the result is 154, represents the actual width of current terminal window.

Please help me find out the right if statement, or gives another solution lead to the same goal.

  • 1
    Move the code to after/plugin/an_arbitrary_name.vim. Or, alternatively, you can move if / endif into the auto command. – Matt Oct 8 at 7:41
  • @muru Not sure if I'm misreading either, but isn't your question about determining the size in pixels of the window, and this question about getting the correct size (in characters) during startup? – Rich Oct 8 at 13:00
  • @Rich ah, yes. I'll retract my vote. – muru Oct 8 at 13:40
  • @Rich Sorry for the vague description, I've updated the title and some of the content, hope this makes it clearer. – Reorx Oct 8 at 13:54
  • @Matt Thanks for the hint, will try. – Reorx Oct 8 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.