2

Let’s say I have multiple STRING occurrences. I want to replace the 1st occurrence with STRING_A, 2nd occurrence with STRING_B, 3rd occurrence with STRING_C.

e.g

Color of my pant is STRING. Color of my hair is STRING. Color of my car is STRING.

After I run search and replace, I should get:

Color of my pant is STRING_A. Color of my hair is STRING_B. Color of my car is STRING_C.

Any help will be greatly appreciated.

  • CTRL-a is used to increment a number, which can be useful in recording and repeating an operation with the number incrementing on the previous. Of course that's numbers and not character incrementing. But it appears that if you `:set nrformats+=alpha`, it will work on characters, too. – Christian Gibbons May 19 '18 at 19:19
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww May 19 '18 at 20:47
  • @jww, don't you want to close these too? https://stackoverflow.com/q/50414736/390913, https://stackoverflow.com/q/11828270/390913, https://stackoverflow.com/q/71323/390913, ... Like almost all `vim` questions? And this one: https://stackoverflow.com/a/21340898/390913. – perreal May 20 '18 at 03:49
  • @perreal - Yes, I've contributed to the problems in the past. – jww May 20 '18 at 04:01
  • @jww, the intricate language of vim is a programming language without a doubt. It is used by almost half of the programmers daily and trying to fight against vim questions is meaningless. And you can still correct your past mistakes. – perreal May 20 '18 at 04:05

2 Answers2

2

From vim wiki:

let @a=1 | %s/STRING/\='STRING_'.(@a+setreg('a',@a+1))/g

But this will give you STRING_1, STRING_2 etc.

Slight modification gives the desired result:

let @a=65 | %s/STRING/\='STRING_'.nr2char(@a+setreg('a',@a+1))/g

If you want to get the substitutions from an array, first define an array:

:let foo=['bar','baz','bak']

Then do the substitution:

let @a=0 | %s/STRING/\=get(foo, @a+setreg('a',@a+1))/g

This will give you:

 Color of my pant is bar. Color of my hair is baz. Color of my car is bak.
perreal
  • 94,503
  • 21
  • 155
  • 181
  • Thank you so much. Is there way STRING_A, STRING_B can come from string array. I had simplified problem. But my original problem is I wanted to replace successive STRING occurrence with different string from array. – Nikhilesh Sarish Kulkarni May 19 '18 at 19:34
  • @NikhileshSarishKulkarni, updated the answer to use an array. – perreal May 20 '18 at 03:45
0

You can define a List of replacements, and then use :help sub-replace-expression to pop replacements off it:

:let r = ['bar', 'baz', 'bak']
:%substitute/STRING/\=remove(r, 0)/g
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324