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"]                                                    

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"
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.