Conditional statements are performed using if statements:
if g:animal_kind == 'cat'
echo g:animal_name . ' is a cat'
elseif g:animal_kind == 'dog'
echo g:animal_name . ' is a dog'
else
echo g:animal_name . ' is something else'
endif
You can also make this operation inline:
echo g:animal_name . (g:is_cat ? 'in'is a cat' : 'in'is something else')
Vim supports all of the logical operators you're used to from other languages:
- && - and
- || - or
- ! - not
For example, you can do this:
if !(g:is_cat || g:is_dog)
echo g:animal_name . ' is something else'
endif
In the previous example, you'll get to g:animal_name . ' is something else' only if neither g:is_cat or g:is_dog are true.
This can also be written with the && operator:
if !g:is_cat && !g:is_dog
echo g:animal_name . ' is something else'
endif
Since text editing implies operating on strings, Vim has additional text-specific comparison operators:
- == compares two string; case sensitivity depends on user's settings (see later)
- ==? explicitly case insensitive comparison
- ==# explicitly case sensitive comparison
- =~ checks a match against an expression on the right (=~? or =~# to make those explicitly case insensitive or sensitive)
- !~ checks that a string does not match an expression on the right (=~#or !~# to make those explicitly case insensitive or sensitive)
The default behavior of ==, as well as =~ and !~ (case sensitive or case insensitive) depends on the ignorecase setting.
Here are some examples:
'cat' ==? 'CAT' " true
'cat' ==# 'CAT' " false
set ignorecase | 'cat' == 'CAT' " true
'cat' =~ 'c.+' " true
'cat' =~# 'C.+' " false
'cat' !~ '.at' " false
'cat' !~? 'C.+' " false