54

I'm aware that I can use / followed by a regex to search something. And I can use ? to search backwards. And I can use n and N to repeat the search forward and backward.

There are also two nice shortcuts: * or # will search for the word under the cursor (forward/backward). That's very useful! (there are also g* and g# variants)

But... Once I've selected text using visual mode (v), how can I ask Vim to search for exactly that text? A quick look at :help did not... huh... help me.

  • 3
    Related to Search for selection in Vim. – Rodrigue Oct 8 '12 at 15:06
  • Thanks for the tips with * and #! Would be nice if your question would also explain what g* and g# would do ;) – winklerrr Oct 25 '18 at 15:28
15

The following sequence will do what you want, given an already selected block of text:

  • y (yank the selected text, into the " register by default)
  • / (enter search mode)
  • (\ V) (optional, enter "very no magic" mode*)
  • Ctrl+r " (insert text from " register)
  • Enter (Engage!)

(*) "very no magic" mode interprets the following text as plain text instead of as a regex. note however that \ and / are still special and will have to be handled in other ways. If the text does not have any characters considered special, you can skip this step.

Source: Vim Tips Wiki

  • 1
    Thank you! This is much more understandable than the "How-To" on the vim.wikia page – winklerrr Oct 25 '18 at 15:25
59

You can yank the hightlighted text first. Then

  • /

  • Ctrlr

  • "

Which will paste what you have yanked after the /.

  • 7
    Nice idea... except that the pasted text will be interpreted as a regex, which is not desired. :-( – Denilson Sá Maia Aug 22 '10 at 16:58
  • 4
    If this is almost good enough for you, then you could do /\V<C-r>" instead. By prepending \V to your search query, you tell vim to use "very no magic". Backslashes may still cause you problems. See :help \V – idbrii Jul 9 '12 at 6:33
  • 3
    It might help to explain that Ctrl+R is how you access a register, and * is simply the system clipboard register. For example, you can also do: yaw to yank a word into register " (the default register), then /<C-r>" to search for that string. – Ben Davis Sep 30 '13 at 17:47
  • 5
    @DenilsonSá - You can use this solution, but instead of using / to do your search, use ? instead, since reverse searches escape slashes by default. Then you can hit N to continue your search in the forward direction. – Brad Parks May 8 '15 at 18:24
7

I never felt the need for such a feature but, considering you can find a need for any feature on Vim, I think that should help:

Search for visually selected text

I didn't test it but, looking at the code, it seems to be exactly what you're searching for.

4

You can find a method to create this behavior here at the vim wiki.

  • Nice, I found the version to escape the slashes to be helpful. vnoremap // y/\V<C-r>=escape(@",'/\')<CR><CR> – craft Jan 28 at 23:23
1

This solution empowers your vim search visually selected context even with multiline and escape characters.

Add the following code in you .vimrc, and search your visually selected content by //. You can also globally substitute the selected content by /s. Or Locally substitute the selected context by // first, and then visually select a region and :'<,'>s//{new_text}.

set incsearch
set hlsearch
set ignorecase
function GetVisualSelection()
  let raw_search = @"
  let @/=substitute(escape(raw_search, '\/.*$^~[]'), "\n", '\\n', "g")
endfunction
xnoremap // ""y:call GetVisualSelection()<bar>:set hls<cr>
if has('nvim')
  set inccommand=nosplit
  xnoremap /s ""y:call GetVisualSelection()<cr><bar>:%s/
else
  xnoremap /s ""y:call GetVisualSelection()<cr><bar>:%s//
endif

The above configuration is only about searching. For all my vim configuration, please visit .vimrc

0

In my configurations on two separate machines, if I select text and then hit / it automatically searches for the selected text.

  • That's not the default behavior. Here, when I press / (while in visual mode), it just shows an empty regex prompt, waiting for me to type something. – Denilson Sá Maia Sep 21 '09 at 10:51
  • Odd, because I never did anything to enable it. – Nathan Fellman Sep 21 '09 at 11:25
  • 3
    Many distros ship a non-standard vim config as the default (probably because the standard vim config disables many useful features, mostly for vi compatibility). – sleske Mar 23 '10 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.