Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim - yank into search register

Tags:

vim

vi

editor

Is there any easy/quick way to "yank" into vim's "last search" register ("/)?

From the vim documentation, it appears that the answer is no, but that it can be assigned via a "let" command:

It is writable with ":let", you can change it to have 'hlsearch' highlight other matches without actually searching.  You can't yank or delete into this register. 

Ideally what I'd like to do is something like:

"/5yw 

which would yank the next 5 words under the cursor & put them in the last search buffer

Alternatively, if there is a way to search for the contents of a named register, that would work too. In other words, if I could do:

"A5yw 

and then search for what is in register A, that would work too.

The closest I can come is to yank into a named register & then copy that register into the last search register, e.g.

"A5yw :let @/=@A 

At the risk of making a long question longer, I want to state that it's not always 5 words I'd like to "yank & search" -- sometimes it's 17 characters, sometimes it's to the end of the line, etc... so a hard-coded macro doesn't give me the flexibility I'd want.

like image 702
Dan Avatar asked Feb 22 '10 17:02

Dan


People also ask

How do I yank to a register in vim?

2.2. Vim is a powerful editor. First, in the Normal mode, we move our cursor on the last word “editor” and press yaw to yank the word editor into the unnamed register “”. Next, we move the cursor to an empty line and press p without giving a register name. We'll see the yanked text “editor” is pasted.

Is there a search function in vim?

Search HistoryVim keeps track of all the search operations you made in the current session. To browse the search history, press / or ? and use the arrow up/down keys to find a previous search operation. To run the search, simply press Enter . You can also edit the search pattern before performing the operation.

How do you search a sentence in vim?

One can search forward in vim/vi by pressing / and then typing your search pattern/word. To search backward in vi/vim by pressing ? and then typing your search pattern/word. Once word found in vim, you can press the n key to go directly to the next occurrence of the word in backwards.

How do I paste from clipboard in vim?

When using Vim under Windows, the clipboard can be accessed with the following: In step 4, press Shift+Delete to cut or Ctrl+Insert to copy. In step 6, press Shift+Insert to paste.


2 Answers

After pressing / to enter a search string, you can then use Ctrl-R and then type the letter representing the register that you want to use.

eg.

  • First, "Ayw to yank a word into register A
  • Then, / ^R A to put the contents of register A into the search string.
like image 188
a'r Avatar answered Oct 03 '22 06:10

a'r


If you did not use any register to store the yanked text vim uses 0 register. You can search for that by typing Ctrl-R 0 after /.

A more complicated example. Say that you want to search in another buffer for the text inside quotes which is under the cursor right now:

  • You can do that with yi" (yank inner quote)
  • Go to the buffer where you want to search
  • Type /Ctrl-R 0
like image 30
csierra Avatar answered Oct 03 '22 07:10

csierra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!