21

How do I search for the selected (with v and y) string? I usually search with /, but I cannot paste the selected string after /.

HaskellElephant
  • 9,819
  • 4
  • 38
  • 67
Sungguk Lim
  • 6,109
  • 7
  • 43
  • 63

5 Answers5

25

In insert mode you can use CTRL-R to insert the contents of Vim registers. By default, copied text gets put in the unnamed register ", so to insert that text you would type <C-R>" in insert mode. The search prompt uses Command-line mode which has its own CTRL-R that works almost identically to the one in Insert mode.

So if I just yanked the text foo, typing /<C-R>" would search for the text foo once I press enter.

Chris Johnsen
  • 214,407
  • 26
  • 209
  • 186
David Brown
  • 13,336
  • 4
  • 38
  • 55
  • 1
    strictly speaking this is not in insert mode but in command line mode. And about this: [see this other answer](http://stackoverflow.com/questions/3997078/how-to-paste-text-into-vim-command-line/3997110#3997110) that explains about registers more in depth. – Benoit Dec 21 '11 at 15:41
  • @Benoit I knew there must be some name for that mode. Updated my answer. – David Brown Dec 22 '11 at 04:01
  • 1
    Just a comment. Pressing Ctrl R showed a " on the command line. I then needed to type " to get the search term... – 576i Jan 09 '18 at 11:05
4
:set hls
:vmap * y:let @/ = @"<CR>
  • set hls (hight light search)
  • v => hjkl (select something)
  • press *, copy selected text to reg "
  • set content of reg / as "
  • press n/N to navigate
BenMorel
  • 34,448
  • 50
  • 182
  • 322
kev
  • 155,172
  • 47
  • 273
  • 272
2

I have this mapping defined in my vimrc, it maps * to defining the search pattern as what is currently highlighted (escaping all potential dangerous characters, and converting a space in what is highlighted to any sequence of spaces)

xnoremap * :<C-U>let old_reg=getreg('"')|let old_regtype=getregtype('"')<CR>gvy/<C-R><C-R>=substitute(substitute(escape(@", '/\.*$^~['), '\s\+', '\\s\\+', 'g'), '\_s\+', '\\_s*', 'g')<CR><CR>gV:call setreg('"', old_reg, old_regtype)<CR>:let v:searchforward=1<CR>

In order to use it, start visual mode with v, and then highlight what you want to search and press * not y.

Of course you can map # to search backwards (exactly the same except that v:searchforward should be set to 0.

Benoit
  • 76,634
  • 23
  • 210
  • 236
  • That mapping gave an error in nvim ("Undefined variable CR"). I use [this](https://vim.wikia.com/wiki/Search_for_visually_selected_text) from the Vim Wiki instead that makes searching for URLs possible, for example: `vnoremap // y/\V=escape(@",'/\')` – Matthias Braun Nov 24 '19 at 11:21
0

First yank the highlighted text using y. Then

  • /
  • Ctrl + r
  • "

Above commands will paste what you have yanked after the /. Then press ENTER

Riyafa Abdul Hameed
  • 7,417
  • 6
  • 40
  • 55
0

If you only care about searches, you can use the cheat method I use. Yank a word via v or y+w, then issue the command

:%s//XYZ/gc 

This will search for the last searched word. Then when you find it, it will ask for confirmation to replace with XYZ, and all you have to do is hit q to quit.

puk
  • 16,318
  • 29
  • 119
  • 199