114

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.

4
  • 4
    Related to Search for selection in Vim.
    – Rodrigue
    Oct 8, 2012 at 15:06
  • 1
    Thanks for the tips with * and #! Would be nice if your question would also explain what g* and g# would do ;)
    – winklerrr
    Oct 25, 2018 at 15:28
  • 1
    @winklerrr prefixing the g character means that you want to search for the same word on any other tab that you have opened in the same VIM session/window. Jul 20, 2021 at 17:02
  • 1
    @alexventuraio Nah, g*/g# means that a match doesn't have to be a whole word (less strict), whereas */# only matches the whole word (more strict). docs For example, using g* when the cursor is on the word test will result in the command /test which would match the first four letters in the word testing. Using * will result in the command /\<test\>, notice the added angle brackets, which means it must match the whole word test therefore it will not match testing like g* would.
    – RcoderNY
    May 31, 2023 at 10:42

8 Answers 8

93

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

3
  • 1
    Thank you! This is much more understandable than the "How-To" on the vim.wikia page
    – winklerrr
    Oct 25, 2018 at 15:25
  • @winklerrr Agreed, this is much quicker to do the first time than the steps described on your linked vim.wikia page. However, for anyone who came here and was disappointed that there wasn't a 'couple-of-keystrokes' answer to this question, that link does provide a way to set up vim to achieve this by typing //.
    – M_M
    Aug 23, 2023 at 15:14
  • 1
    P.S. For those of you like me who are "capitalizationally-challenged", that's a capital 'V' for 'very nomagic'. Lowercase would mean 'very magic'. Big difference. :help magic Sep 4, 2023 at 2:19
90

You can yank the hightlighted text first. Then

  • /

  • Ctrlr

  • "

Which will paste what you have yanked after the /.

4
  • 9
    Nice idea... except that the pasted text will be interpreted as a regex, which is not desired. :-( Aug 22, 2010 at 16:58
  • 5
    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, 2012 at 6:33
  • 4
    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, 2013 at 17:47
  • 9
    @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, 2015 at 18:24
13

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.

3
  • 1
    I did this for * to make it consistent * usage elsewhere.
    – Sam Habiel
    Mar 8, 2021 at 17:27
  • If there's yanked text before calling this command using just y, p won't paste it anymore.
    – WalksB
    Jul 14, 2021 at 20:49
  • Here's another handy mapping vim.keymap.set('v', '/', "\"fy/\\V<C-R>f<CR>" ) for NVIM users Sep 12, 2022 at 20:40
5

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

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

  • y (yank, or copy the selected text, into the " register by default)

  • ? (enter search mode, searching in the backward direction)

  • Ctrlr " (paste what's in " register into your search)

  • Enter (start searching)

This will work for many situations where / wont, because using ? does a reverse search, which escapes slashes by default. Then you can hit N to continue your search in the forward, direction, and n to search in the backward direction if you want.

Also, this is a variant to these great answers elsewhere on this page answer 1, answer 2.

2

The other answers here break p paste, since they override the unnamed register ". Here is a solution that doesn't have this problem:

vnoremap ml :<c-u>let temp_variable=@"<CR>gvy:<c-u>let @/='\V<C-R>=escape(@",'/\')<CR>'<CR>:let @"=temp_variable<CR>:<c-u>set hlsearch<CR>

This solution also doesn't jump to the next instance of the pattern, which I find disruptive if it is out of frame in the current window.

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

1
  • Beautiful! I modified it a bit so that it wouldn't override p paste after calling it, and now it is perfect.
    – WalksB
    Jun 28, 2022 at 0:45
0

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

3
  • 1
    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. Sep 21, 2009 at 10:51
  • Odd, because I never did anything to enable it. Sep 21, 2009 at 11:25
  • 5
    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, 2010 at 10:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .