If I have the following content
head = cfg["head"]
head = cfg["head"]
head = cfg["head"]
head = cfg["head"]
Assuming I have a list as below
:let list = ['head', 'hhea', 'os2', 'post']
How can I transform that content into
head = cfg["head"]
hhea = cfg["hhea"]
os2 = cfg["os2"]
post = cfg["post"]
2 Answers 2
You could consider using g together with execute
:let list = ['head', 'hhea', 'os2', 'post']
:let i = 0
:'<,'>g/head/exe 's/head/' . list[i] . '/g' | let i += 1
You could also consider the following approach (put, map), although it feels a bit hard‐coded
:pu = map(list, {i, val -> printf('%s = cfg["%s"]', val, val)})
In this case it works, but the contents of the list will be modified
'<,'>g/head/exe "s/head/" . list->remove(0) . "/g"
Sign up to request clarification or add additional context in comments.
Comments
Taking your example, if you can first visual select those lines, this :s command may help you:
'<,'>s/head/\=list[line('.')-line("'<")]/g
Note This command doesn't check if the number of lines you selected matches the number of elements in the list
answered Dec 18, 2025 at 0:18
Kent
197k36 gold badges248 silver badges317 bronze badges