Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the vim script syntax to test the value of a (boolean) option?

Tags:

vim

I'm trying to add a conditional to my vimrc file testing if number (line numbers) option is set. I have :number added only with HTML file type,

:autocmd FileType html source $HOME/.vim/html_vimrc.vim

:if ('&number' == 0)  "IF number is off
: set spell           "turn on spell checker
:else                 "ELSE file type is HTML
:  set nospell        "turn spell checker off 
:endif

I've tried,

:if (&number == "number")

:if $number

I've tried with parentheses, quotes, no quotes. All my tests either set spell for all or none. I'm reading the VIMDOCS on conditionals, but I'm not getting something right and I can't find a similar example.


Update: In addition to testing Michael's code below, I've simplified the test with the following in vimrc:

:if exists("g:prog")
:  set number          "trying a different setting
:endif

And in the FileType loaded html_vimrc file I have,

"number                "turned this off 
:let g:prog=1

In this situation the test case of test.html the g:prog is set but "number" is not. In the test.vim case g:prog is not set and "number" is not. This seems to suggest there is some fundamental logic to how VIM handles loaded vimrc files that I am missing.

like image 724
xtian Avatar asked Sep 17 '25 05:09

xtian


1 Answers

Use &number to test a boolean, without quotes.

if (&number == 1)

Or more simply:

if (&number)

So your entire operation looks like:

if (&number == 0)  "IF number is off
 set spell           "turn on spell checker
else                 "ELSE file type is HTML
  set nospell        "turn spell checker off 
endif
like image 130
Michael Berkowski Avatar answered Sep 18 '25 20:09

Michael Berkowski