So let's see how over-engineered we can get:
Let's say that an easier way to communicate to vim the words to process is to get them in a vimscript list. We can then simply concatenate the list with the \|
operator to create the desired pattern and apply the substitution that @Tumbler41 suggested in his answer:
function! GetWords()
" List the items to process
let list = ['Apple', 'Cabbage', 'Turtle']
" Concatenate the list to make a pattern
let pattern = list[0]
for index in range(1, len(list)-1)
let pattern = pattern . '\|' . list[index]
endfor
" Ashamedly stealing @Tumbler41 solution
execute '%s/\('.pattern.'\)\s\+\(\d\+\)/\1 = \2;/'
execute 'v/'.pattern.'/d'
endfunction
This way you can simply open the buffer containing the text to change and use :call GetWords()
.
Cool, but can we make it even more over-complicated? Sure!
Lets say that the ultimate easiest way to get the list of words to process is to simply write them in a buffer.
You'll have 2 buffer, the one you showed in the question and a second one containing
Apple
Cabbage
Turtle
With this function:
function! GetWords(bufList, bufModify)
" Go to the buffer containing the list
execute 'b' . a:bufList
" List the items to process
let list = getline(1, '$')
" Concatenate the list to make a pattern
let pattern = list[0]
for index in range(1, len(list)-1)
let pattern = pattern . '\|' . list[index]
endfor
" Go to the buffer to modify
execute 'b' . a:bufModify
" Ashamedly stealing @Tumbler41 solution
execute '%s/\('.pattern.'\)\s\+\(\d\+\)/\1 = \2;/'
execute 'v/'.pattern.'/d'
endfunction
You can simply use :call GetWords(3, 4)
where 3
and 4
are the number of the buffers containing the list and containing the text to modify and the function will do the transformation.
Credits goes to @Tumbler41 for the substitution command :-)