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.

5
  • 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. Commented May 19, 2018 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 in the Help Center. Perhaps Super User or Unix & Linux Stack Exchange would be a better place to ask. Commented May 19, 2018 at 20:47
  • @jww, don't you want to close these too? stackoverflow.com/q/50414736/390913, stackoverflow.com/q/11828270/390913, stackoverflow.com/q/71323/390913, ... Like almost all vim questions? And this one: stackoverflow.com/a/21340898/390913. Commented May 20, 2018 at 3:49
  • @perreal - Yes, I've contributed to the problems in the past. Commented May 20, 2018 at 4: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. Commented May 20, 2018 at 4:05

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

2 Comments

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.
@NikhileshSarishKulkarni, updated the answer to use an array.
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

Comments

Your Answer

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.