O'Reilly logo

Mastering Vim by Ruslan Osipov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Conditional statements

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: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required