For example, I have a text , 10 3 4 2 10 , 4 ,10 ....
No I want to change each 10 with different words
I know %s/10/replace-words/gc but it only let me replace interactively like yes/no but I want to change each occurrence of 10 with different words like replace1, 3, 4 , 2 , replace2, 4, replace3 ....
Replaces each occurence of
10
withreplace{index_of_match}
:Replaces each occurence of
10
with a word from a predefined array:Replaces each occurence of
10
with a word from a predefined array, and the index of the match:But since you have to type in any word anyway, the benefit of the second and third function this is minimal. See the answer from SpoonMeiser for the "manual" solution.
Update: As wished, the explanation for the regex part in the second example:
%
= on every line in the documents/<search>/<replace>/g
=s
means do a search & replace,g
means replace every occurence.\=
interprets the following as code.remove(b, 0)
removes the element at index 0 of the listb
and returns it.so for the first occurrence. the line will be
%s/10/foo/g
the second time, the list is now only['bar', 'vim']
so the line will be%s/10/bar/g
and so onNote: This is a quick draft, and unlikely the best & cleanest way to achieve it, if somebody wants to improve it, feel free to add a comment
Is there a pattern to the words you want or would you want to type each word at each occurrence of the word you're replacing?
If I were replacing each instance of "10" with a different word, I'd probably do it somewhat manually:
Which doesn't seem too onerous, if each word is different and has to be typed separately anyway.