syntagma
syntagma

Reputation: 24294

Searching using previously defined keywords in Vim

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?

Upvotes: 2

Views: 65

Answers (3)

Ben
Ben

Reputation: 8905

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.

Upvotes: 2

lcd047
lcd047

Reputation: 5851

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.

Upvotes: 5

roblogic
roblogic

Reputation: 1316

You could put the keywords in a file and then :source filename. But generally when searching it's easier to hit / and then use the up/down arrow keys to use previous search terms.

Upvotes: 2

Related Questions