Skip to Content
Mastering Vim
book

Mastering Vim

by Ruslan Osipov
November 2018
Beginner
330 pages
7h 21m
English
Packt Publishing
Content preview from Mastering Vim

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 ? ' is a cat' : ' 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Vim

Mastering Vim

Damian Conway
Vim Masterclass

Vim Masterclass

Jason Cannon
Modern Vim

Modern Vim

Drew Neil

Publisher Resources

ISBN: 9781789341096Supplemental Content