Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching using previously defined keywords in Vim

Tags:

vim

search

There are some keywords I search frequently for throughout my project. I would like to use search like this: I put some keyword into the "register", after a while (I may or may not use regular search during that time) I want to search for that keyword in the currently open file. So I load it from the register and then n/N movements work like usual, just that they search for the keyword I have loaded.

Is that possible?

like image 345
syntagma Avatar asked Nov 30 '25 12:11

syntagma


2 Answers

Try q/, this will open a window with your search history. There you can pick a pattern, edit it, and it will get run when you press Enter.

On a side note: q: opens a similar history window for commands.

like image 131
lcd047 Avatar answered Dec 02 '25 05:12

lcd047


You can do exactly what you want, actually.

Vim has 26 "named registers" (a-z) which you can use for recording macros or storing text. You can use these registers as part of commands, for example to copy the word under the cursor into register 'a', type "yiw in normal mode. You can also assign to them directly, for example to put the word "snarfblat" into register 'b' use :let @b='snarfblat' (and press Enter).

Once you have text in a register, you can retrieve it in a search using Ctrl+R and then pressing the register you want to retrieve. So in your case to search for the contents of register 'a' simply start a search with /, press Ctrl+R followed by a to insert the register text, then press Enter to do the search as normal.

See :help registers for details.

like image 40
Ben Avatar answered Dec 02 '25 03:12

Ben



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!