Viml loops: Difference between revisions

From wikinotes
 
Line 14: Line 14:
endfor
endfor
</source>
</source>
<syntaxhighlight lang="vim">
" Iterate numbers in range
for item in range(1, 3)
    echom item
endfor
</syntaxhighlight>


<source lang="vim">
<source lang="vim">

Latest revision as of 22:50, 2 January 2023

For Loops

" Iterate chars in string
for item in split("abcd", '\zs')
    echom item
endfor
" Iterate Through Lists
for l in ['a','b','c']
	echo l
endfor
" Iterate numbers in range
for item in range(1, 3)
    echom item
endfor
"Iterate Through Dicts
for [next_key, next_val] in items(dict)
    let result = process(next_val)
    echo "Result for " next_key " is " result
endfor

While Loops

" Index based iteration
let i=0
while i < 10
	echo i
	i +=1
endwhile