1

I'm trying to show / hide horizontal scrollbar conditionally, depending on wrap option:

  • with wrap - show horizontal scrollbar
  • with nowrap - hide

Here is what I tried, but for some reason it doesn't work:

set guioptions+=%{&wrap?'b':''}
1

I guess you want the horizontal scrollbar on nowrap. You can handle this with a autocmd, like:

autocmd OptionSet wrap if &wrap|set guioptions-=b|else|set guioptions+=b|endif

If you change the option wrap during runtime, this will automatically switch on/off the horizontal scrollbar. The autocommand is not triggered on startup (sourcing vimrc), see :help OptionSet.

In your vimrc, you should define your initial settings like:

set nowrap
...
if &wrap
    set guioptions-=b
else
    set guioptions+=b
endif

This way, you just need to change wrap and guioptions will be changed as needed. Order is important here.

  • Thanks. For some reason it doesn't work for me (does it work for you?). I have only two lines in vimrc currently: set wrap (set nowrap) and this one. – james Feb 10 at 10:21
  • @james See update. – Ralf Feb 10 at 10:57
  • This should work in vimrc if you place the autocmd early enough, before the set wrap, in which case it'll be triggered. – filbranden Feb 10 at 16:23
  • 1
    @filbranden Doesn't work in vimrc. The docs say "Is not triggered on startup...". – Ralf Feb 10 at 16:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.