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

Loops

You can loop through lists and dictionaries using the for keyword. For example, do this to go through a list:

for animal in animals  echo animalendfor

And here's you iterating through a dictionary:

for animal in keys(animal_names)  echo 'This ' . animal . '''s name is ' . animal_names[animal]endfor

You can also access both the key and the value of the dictionary simultaneously using items:

for [animal, name] in items(animal_names)  echo 'This ' . animal . '''s name is ' . nameendfor

You can control the iteration flow with continue and break. Here's an example of using break:

let animals = ['dog', 'cat', 'parrot']for animal in animals  if animal == 'cat'    echo 'It''s a cat! Breaking!'    break  endif echo 'Looking at a ' . animal . ', it''s ...

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