How to search for selected text in Vim?

62

13

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.

Denilson Sá Maia

Posted 2009-09-15T01:55:34.043

Reputation: 9 603

Thanks for the tips with * and #! Would be nice if your question would also explain what g* and g# would do ;) – winklerrr – 2018-10-25T15:28:02.357

3

Related to Search for selection in Vim.

– Rodrigue – 2012-10-08T15:06:24.350

Answers

25

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

glennsl

Posted 2009-09-15T01:55:34.043

Reputation: 366

1

Thank you! This is much more understandable than the "How-To" on the vim.wikia page

– winklerrr – 2018-10-25T15:25:40.627

67

You can yank the hightlighted text first. Then

  • /

  • Ctrlr

  • "

Which will paste what you have yanked after the /.

hoge

Posted 2009-09-15T01:55:34.043

Reputation: 671

6@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 – 2015-05-08T18:24:38.243

7Nice idea... except that the pasted text will be interpreted as a regex, which is not desired. :-( – Denilson Sá Maia – 2010-08-22T16:58:35.137

4If 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 – 2012-07-09T06:33:19.807

3It 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 – 2013-09-30T17:47:32.913

7

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

vnoremap // y/\V<C-R>=escape(@",'/\')<CR><CR>

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

Alexandre Moreira

Posted 2009-09-15T01:55:34.043

Reputation:

4

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

akf

Posted 2009-09-15T01:55:34.043

Reputation: 3 781

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

0

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

Charles X.-T. Li

Posted 2009-09-15T01:55:34.043

Reputation: 1

0

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

Nathan Fellman

Posted 2009-09-15T01:55:34.043

Reputation: 8 152

3Many 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 – 2010-03-23T10:08:38.457

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 – 2009-09-21T10:51:33.370

Odd, because I never did anything to enable it. – Nathan Fellman – 2009-09-21T11:25:06.593