Loops and Comparisons

VimL has a while loop. It starts with while and a condition, and it ends with endwhile:

intro/loop.vim
 
let​ animalKingdom = [​'Crocodile'​, ​'Bug'​, ​'Octopus'​, ​'Penguin'​]
 
 
while​ len(animalKingdom) > 0
 
echo animalKingdom[0] . ​' Friend'
 
call​ remove(animalKingdom, 0)
 
endwhile​​​
 
​​
 
" → Crocodile Friend
 
" Bug Friend
 
" Octopus Friend
 
" Penguin Friend

The condition here, len(animalKingdom) > 0, checks that the size of animalKingdom is greater than 0. To do that, it uses len. On a List, this function returns the number of items. It can also be used to get the length of a String value, but there’s a dedicated function, strlen, for that.

In the body of the while loop, the first statement, echo ...

Get The VimL Primer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.