Sometimes you want to search for a long string in Vim. When using a GUI interface you may be able to get away with CTRL+c, CTRL+v but when you are in a terminal, it is a little trickier to use Vim Visual Mode search for a selection. Here is how to search in Vim using Visual Mode to select the search string.
- Select the text in visual mode (
vand move cursor to highlight)
- Yank the text (
- Go into search mode (
- Select registers (
- Select the unnamed register, i.e. the last yank or delete (
You never use visual mode?
Then you aren’t leveraging all the power of Vim. To be fair, I don’t think I know anyone leveraging all the power of Vim but visual mode is a great tool to add to your utility belt.
Using the * Command
Before jumping into Visual Mode (with
v), you may be able to do your search without entering Visual Mode. When you press
Shift + 8) in Normal mode, Vim will search for the word (i.e.
keyword) under the cursor. Assuming the text you want to search is a single word, this is a great way to search.
Dušan Dimitrić says
Dušan D. – https://dusandimitric.com
Wojciech Wisowaty says
Use # (Shift + 3) to search for the word under the cursor backwards.
Iron Rodge says
Select registers seem to not work anymore for Vim..
Select registers (CTRL+r)
Select the unnamed register, i.e. the last yank or delete (“)
However (Shift+Insert) works for pasting the Yank. It’s a reach for keyboards but it still works.
Sal Ferrarello says
That’s interesting, I don’t seem to be having the same experience. What version of Vim are you using?
I’ve tested on both: Vim 8.2 and Neovim 0.5.0 on my Mac, and in both cases the instructions in this article work for me.
I would double-check you are hitting
/(to start defining a search) before (
seems this operation sequence just for `copy from select` & `paste into cmd line`.
the magic things in text (such as /, .) still need handle (escape)