123

I use Vim and Vim plugins for Visual Studio when writing C++. Often, I find myself wanting to search for a string within a function, for example every call to object->public_member.memberfunc().

I know Vim offers a convenient way to search for a single word, by pressing * and #, and it can also search for typed strings using the ubiquitous slash / command. When trying to search for all the instances of a longer string like the one above, it takes a while to re-type after /.

Is there a way to search for the selection? For example, highlight with v, then copy with y, is there a way to paste after /? Is there an easier shortcut?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Marcin
  • 12,245
  • 9
  • 42
  • 49
  • Possible duplicate of [How do I search for the selected text?](http://stackoverflow.com/questions/8587136/how-do-i-search-for-the-selected-text) – acm Sep 13 '16 at 17:41
  • There's also the [visual-star-search plugin](https://github.com/bronson/vim-visual-star-search) to do exactly this – mattb Feb 04 '22 at 14:07
  • And https://github.com/junegunn/vim-slash which does this plus a few other things. – Marc Mezzarobba Mar 20 '23 at 15:54

6 Answers6

164

Check this Vim tip: Search for visually selected text

Or you can simply yank the selected text with y and go to search mode /, then you can paste the last yanked text with Ctrl+R 0

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 5
    @CMS. Thanks. Great tip! Using :reg to see what's in your registers and then using cntl-R N to get what's in register N is excellent! – Rob Wells Dec 15 '08 at 20:22
109

Answer

  1. Yank the text you want to search for
  2. q/p
  3. Enter

Explanation

q/ works similarly to vanilla search / except you're in command mode so p actually does "paste" instead of typing the character p. So the above will copy the text you're searching for and paste it into a search.

For more details type :help q/

Cory Klein
  • 51,188
  • 43
  • 183
  • 243
  • 4
    I like this one, because I find typing so unconvenient. – Jan Mar 05 '13 at 20:40
  • Excellent! Using emacs in `evil-mode`, this actually works, whereas the accepted answer breaks miserably (probably because of the use of the `Ctrl` key). – eestrada Jan 25 '16 at 23:25
  • 2
    Great for simple strings, but doesn't escape regex special characters, as noted in [this other answer's comment](http://stackoverflow.com/questions/363111/search-for-selection-in-vim#comment44689091_14747639). – Bluu May 20 '16 at 22:44
  • 2
    And you don't even need to yank it. Select it on visual, `q/gvp`, where `gv` stands for last visual selection... –  Apr 25 '18 at 18:20
  • 1
    @nwildner, `q/gvp` doesn't work for me. Could you clarify your suggestion? – bam Feb 25 '19 at 15:57
  • That's what I needed ) – Sergius Feb 04 '22 at 09:03
40

Use q / instead of just /. (Same with q :). Now you can VIM-edit through your command and search history! (Try Ctrl-N and Ctrl-P sometime).

12

I just learned (through the excellent book Practical Vim) that there is a plugin for that. You can find the plugin on GitHub.

The plugin lets you search for a visual selection with * and #.

Raven
  • 2,951
  • 2
  • 26
  • 42
cutemachine
  • 5,520
  • 2
  • 33
  • 30
  • 7
    Note that the other answers don't do any escaping. Using those methods, any selected metacharacters will not be searched for literally, depending on the current `magic` setting. This could even make the search fail for metacharacters that require balancing. The plugin mentioned here will do escaping before searching for the selected text. – Kurt Hutchinson Jan 26 '15 at 21:17
  • 1
    it works, but unfortunately, you cannot use `n` to go to the next found item, while Cory's and CMS's approaches do allow.. – Ayrat Oct 26 '16 at 06:54
  • 2
    This version of the plugin on github is more recent (https://github.com/bronson/vim-visual-star-search). `n` and `N` work as expected for next and previous. – Christian Long Jan 22 '21 at 20:25
  • I took the liberty of changing the link to the repo to https://github.com/bronson/vim-visual-star-search (as Christian Long already pointed out, this is the more recent version of the plugin). In case anyone is interested, the old link pointed to https://github.com/nelstrom/vim-visual-star-search – Raven Aug 12 '21 at 15:18
3

You can actually select text visually and press * and # to search for the next occurrence... It will work the same, the only caveat is that:

Whitespace in the selection matches any whitespace, when searching (searching for "hello world" will also find "hello" at the end of a line, with "world" at the start of the next line).

http://vim.wikia.com/wiki/Search_for_visually_selected_text

AhHatem
  • 1,415
  • 3
  • 14
  • 23
0

--> if you want to highlight a text occurrences in gvim

  1. Select the text & copy
  2. then ?paste the selected text (Note: This will not work for insert mode)