How can I do something like "for char in string" in Vim script?
Asked Answered
C

2

12

I've come up with this, but is there simple way I'm missing?

for s:char in split(s:string, '.\zs\ze.')
Cayla answered 17/7, 2013 at 5:31 Comment(0)
U
18

The canonical way is

for s:item in split(s:mylist, '\zs')
Undercover answered 17/7, 2013 at 6:28 Comment(0)
C
0

In vim >= 8.2.2658 (74e54fcb44), a string can be used in a vim for loop to use each character in turn:

 $ vim -u NONE -E \
   -c 'redir! >/dev/stdout' \
   -c 'for i in "abc" | echo i | endfor' \
   -c quit \
   </dev/null
a
b
c

Documentation was released in 9.0.0336 (9b03d3e75), in :help :for

When {object} is a |String| each item is a string with one character, plus any combining characters.

Caspar answered 13/7, 2024 at 6:6 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.