Search for selection in Vim
Asked Answered
S

6

124

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?

Snuffbox answered 12/12, 2008 at 15:43 Comment(3)
Possible duplicate of How do I search for the selected text?Macy
There's also the visual-star-search plugin to do exactly thisQuackenbush
And github.com/junegunn/vim-slash which does this plus a few other things.Multiversity
Q
166

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

Quarterage answered 12/12, 2008 at 15:45 Comment(1)
@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!Arcade
A
114

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/

Arnaldo answered 4/5, 2012 at 16:11 Comment(6)
I like this one, because I find typing <ctrl-r> so unconvenient.Hurlbut
Excellent! Using emacs in evil-mode, this actually works, whereas the accepted answer breaks miserably (probably because of the use of the Ctrl key).Vanadous
Great for simple strings, but doesn't escape regex special characters, as noted in this other answer's comment.Adrenocorticotropic
And you don't even need to yank it. Select it on visual, q/gvp, where gv stands for last visual selection...Meliamelic
@nwildner, q/gvp doesn't work for me. Could you clarify your suggestion?Catalyst
That's what I needed )Antediluvian
J
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).

Jarnagin answered 16/12, 2008 at 1:11 Comment(0)
D
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 #.

Doubles answered 7/2, 2013 at 9:25 Comment(4)
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.Emanuele
it works, but unfortunately, you cannot use n to go to the next found item, while Cory's and CMS's approaches do allow..Disbranch
This version of the plugin on github is more recent (github.com/bronson/vim-visual-star-search). n and N work as expected for next and previous.Jacquetta
I took the liberty of changing the link to the repo to 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 github.com/nelstrom/vim-visual-star-searchPurapurblind
B
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

Braided answered 13/1, 2013 at 10:32 Comment(2)
for me, selecting text visually then pressing * just extends my visual selection.Diphenylhydantoin
You cannot by default unless you put into vimrc the mapping from the page..Disbranch
R
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)
Ryurik answered 25/1, 2023 at 5:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.