4

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.

xtian
  • 2,765
  • 7
  • 38
  • 65

2 Answers2

7

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
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • I used the same syntax without success. In fact, in my delirium I typed == 1 to turn spell check on for html checked it, then checked it == 0. In my test case of `$vim test.html` and `$vim test`, the result was both files with or without (yes-yes or no-no). (The desired case is of course (yes-no)). %-( – xtian Mar 17 '12 at 01:36
  • ALSO, if it matters, I have another post about [vimrc file syntax weirdness](http://stackoverflow.com/questions/9660627/why-does-this-uncommon-vim-dot-concatenation-syntax-work-while-my-common-vim-sy). They may be related? – xtian Mar 17 '12 at 01:43
1

I simplified the situation to

:if exists("g:prog")
:  let g:ok_prog=1          "trying a different setting
:endif

Then I used :let to check if the variable is available to the different filetype use cases. This shows the variable from the .vimrc file loaded before the autoloaded g:var. (If someone else has a VIMDOCS reference that would be awesome). All of the conditionals fail because neither my :number option nor the g:var are loaded before the conditional fails.

xtian
  • 2,765
  • 7
  • 38
  • 65