John22
John22

Reputation: 33

Search for "bar" while using norm! in Vim

I have a text that looks like this:

foo x1
ddd
ggg
...
bar

I want to yank the word after foo, go to the line were bar is, replace bar with txt, and paste the yanked word.

I need to replace in this code the "j" with something that searches for "bar"

:/^foo/norm! wywj0rtlrxlrt"0p

Now I get this result:

foo x1
txtx1. 
ggg
...
bar

Thank you!

Upvotes: 3

Views: 338

Answers (1)

user852573
user852573

Reputation: 1750

Maybe you could simply replace the motion j with /bar.

To tell :normal that you want to hit Enter and validate your search, you can insert a literal carriage return on the command-line by pressing C-v then Enter, and it would give this command:

:norm! wyw/bar^M0rtlrxlrt"0p

^M is the caret notation of a literal carriage return.

Or you could wrap the whole command inside a non-literal string (surrounded by double quotes), in which you can express a carriage return with the notation \<cr> or \r, and execute the contents of the string with :execute. It would give:

:exe "/^foo/norm! wyw/bar\r0rtlrxlrt\"0p"

Here, \r stands for the carriage return necessary to validate your search, and since the string must use double quotes, the double quote of the copy register ("0) must be escaped.

Upvotes: 3

Related Questions