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
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 .
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.
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.
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/.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With