Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vi vim substitute text in register

Tags:

linux

vim

vi

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

like image 646
Martin Kuester Avatar asked Aug 27 '20 17:08

Martin Kuester


People also ask

How do I replace text in vi?

Press y to replace the match or l to replace the match and quit. Press n to skip the match and q or Esc to quit substitution. The a option substitutes the match and all remaining occurrences of the match. To scroll the screen down, use CTRL+Y , and to scroll up, use CTRL+E .

How do you replace a word with another word in Vim?

Search for text using / or for a word using * . In normal mode, type cgn (change the next search hit) then immediately type the replacement. Press Esc to finish. From normal mode, search for the next occurrence that you want to replace ( n ) and press . to repeat the last change.

How do I replace a word in vi EDitor globally?

The % is a shortcut that tells vi to search all lines of the file for search_string and change it to replacement_string . The global ( g ) flag at the end of the command tells vi to continue searching for other occurrences of search_string . To confirm each replacement, add the confirm ( c ) flag after the global flag.

What command is used to replace a string with another string in vi EDitor?

After running a search once, you can repeat it by using n in command mode, or N to reverse direction. When you want to search for a string of text and replace it with another string of text, you can use the syntax :[range]s/search/replace/.


1 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

like image 143
heijp06 Avatar answered Oct 07 '22 07:10

heijp06