6

I want to

  • Select a word with VISUAL mode
  • Press a key to search for the selected word
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
songyy
  • 4,323
  • 6
  • 41
  • 63
  • Search for the words in the same order as they were selected, as a sentence, or any of the words selected? I.e. selecting `the foo was named bar` and search using it, would only find the same sentence, not sentences containing `the/foo/was/named/bar`? – timss May 28 '13 at 02:48
  • @timss: yeah search for exactly what's selected; – songyy May 28 '13 at 03:05

5 Answers5

13

This has been explained in Search for selection in vim.

  1. Select the text using a visual selection, e.g. v
  2. Yank it, y
  3. Search, /
  4. Paste the last yanked text using <C-r>0 (Ctrlr+0)
    Actually inserts the content from register 0, see :h i_ctrl-r (thanks to romainl in the comments)

Another example is using the command line:

  1. Select the text using a visual selection, e.g. v
  2. Yank it, y
  3. Enter command-line mode with editing an Ex command, q/
  4. Paste yanked text, p, and search by pressing Enter

In short: yq/p<Enter>

Community
  • 1
  • 1
timss
  • 9,982
  • 4
  • 34
  • 56
  • I can't seem to find what `Ctrl-r 0` really means, except that it pastes the last yanked text. `:h Ctrl-r` tells me that it's related to `redo`, but is that right? Could anyone explain how this works or possibly point me to the right help section if it's not redo? – timss May 28 '13 at 03:23
  • 3
    In insert mode, `0` is used to insert the content of register 0. You can also insert the result of an expression: `=2+57`, `=expand('%')`, etc. Read `:h i_ctrl-r`. Note the `i_` for insert mode commands. – romainl May 28 '13 at 05:36
4

Oh, mate, actually you can use the mighty Visual Search originated by the author of the book Practical Vim. It's very lightweight!

Simply add following stuff into your vimrc,

xnoremap * :<C-u>call <SID>VSetSearch()<CR>/<C-R>=@/<CR><CR>
xnoremap # :<C-u>call <SID>VSetSearch()<CR>?<C-R>=@/<CR><CR>

function! s:VSetSearch()
    let temp = @s
    norm! gv"sy
    let @/ = '\V' . substitute(escape(@s, '/\'), '\n', '\\n','g')
    let @s = temp
endfunction

Visual select the content you want to do search, then press the star key *, Voila !

3

Bairui/Dahu's SearchParty has a couple of nifty mappings that build on the "yank and insert" method but deal cleanly with newlines and such:

* Searches for the next occurrence of the currently selected visual text.

# Searches for the prior occurrence of the currently selected visual text.

& Starts a :substitute using the currently selected visual text. 

If you feel a plugin is overkill, it's easy to pull the relevant line from the script and put it in your ~/.vimrc.

romainl
  • 186,200
  • 21
  • 280
  • 313
1

To search for more than one word at a time, the vim search supports or '\|'. So to search for dog cat and bird you can:

/dog\|cat\|bird

or better (exact word match):

/\<dog\>\|\<cat\>\|\<bird\>
cforbish
  • 8,567
  • 3
  • 28
  • 32
1

Not sure why @romainl reply is not upvoted more. It is faster, automatically escapes characters that will mess up search, and vanilla vim. Combined with cgn followed by ., it is a perfect substitute for those that miss Ctrl-d in vscode.