vi vim substitute text in register

Viewed 256

Learning vi/vim and Linux and learning use of :reg and :let. My question is how to substitute and replace (edit) text of a register? I've searched online and in vi/vim help and made some attempts without expected results...

Ex:

Register a:
"1.  This is a sentence.  This is another sentence.  This is yet another sentence."

:.s/This/That/g - replaces all occurrences of "This" with "That" for the entire line when text is in the file.

How to substitute "This" with "That" when in register a?

Thank you in advance for suggestions or answers...

Martin

2 Answers

With \= you can substitute an expression. That expression can be the value of the getreg() function.

So to substitute This with the contents of register a:

:%s/This/\=getreg("a")/g

or

:%s/This/\=@a/g

As you can see you can also use "at" to get the content of a register

I don't know if this is exactly what you want, but give it a try:)

:let @a = substitute(@a, "This", "That", "g")

for more details, you can type

:h substitute()
Related