Vim - yank into search register
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.
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
I'm using following code for that:
vnoremap <silent>* <ESC>:call VisualSearch('/')<CR>/<CR>
vnoremap <silent># <ESC>:call VisualSearch('?')<CR>?<CR>
function! VisualSearch(dirrection)
let l:register=@@
normal! gvy
let l:search=escape(@@, '$.*/\[]')
if a:dirrection=='/'
execute 'normal! /'.l:search
else
execute 'normal! ?'.l:search
endif
let @/=l:search
normal! gV
let @@=l:register
endfunction
Searching for a selection:
if you want to first yank a section of a line, then use "v" and move with cursors until you have marked what you want, then press y for yank and now the selection is in register 0
you can then type /Ctrl-R 0
So basically an extended version of the # and * commands, right? It sounds like you want to define a custom operator (a command that expects a motion). I've never actually done this, but I did find a plugin which looks like it might make it easier to do so. There are some examples provided.