0

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"] 
romainl
199k22 gold badges301 silver badges340 bronze badges
asked Dec 2, 2025 at 6:21

2 Answers 2

1

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"
romainl
199k22 gold badges301 silver badges340 bronze badges
answered Dec 2, 2025 at 6:21
Sign up to request clarification or add additional context in comments.

Comments

1

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

enter image description here

answered Dec 18, 2025 at 0:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.