`*`-key usage (set the search pattern to the main selection)

Hi!

Kakoune newbie here. I see, that using *-key I’m saving the current selection to the / register, so I can use n/a-n (etc) to browse through available search results.

And here is an excerpt from Trampoline:

The smart search primitive (*) uses the current
selection as a search pattern, which will be saved to the
/ register. In order to use this primitive to execute a
temporary search, one could make this primitive save the
pattern to a different register, to preserve the default one,
e.g., "m* to save the pattern to the m register, or even
"_* to save the pattern to a “null” register, which does
not store anything written to it.

I really don’t understand why I should save * to a temporary register first. Any example of why this is useful?

Thank you!

A good reason to save your current search term (i.e., the current selection, via *, or a search term that you type yourself) into the / register is that the register keeps a history of recent search terms (100 search terms, according to :doc registers).

This history can later be accessed by typing <c-p> after /. Try it, and you will see that typing <c-p> shows your previous search terms after the prompt. This makes it easy for you to search again for one of these terms – no need for retyping.

Summing up, the history stored in the / register saves keystrokes :slight_smile:

Thanks a lot. I think I understand why it is useful to save current selection into the / register. The question was: why Trampoline recommends to save * into temporary register first?

In order to use this primitive to execute a
temporary search, one could make this primitive save the
pattern to a different register, to preserve the default one,
e.g., "m* to save the pattern to the m register, or even
"_* to save the pattern to a “null” register, which does
not store anything written to it.

How saving * to “black hole” register helps preserve the search pattern default contents?

Oh, I understand – but now I don’t know for sure how to explain what the Trampoline says. I guess that in some cases you do not want your search term to pollute the search history.

But, theses cases would involve scripts in which you search for a term “behind the scenes” before doing something else on your buffer. In scripts, however, executing a command such as

execute-keys / my_secret_term <ret>

will not pollute the search history anyway, because by default the execute-keys command saves common registers (such as /) and restores them once the keys have been executed (see :doc execeval). So the Trampoline leaves me as puzzled as you are…

1 Like

I haven’t been using Kakoune long enough to know, but I suspect that once upon a time Kakoune’s * might have behaved like Vim’s *, which sets the search register and jumps to the next match, something like *n in Kakoune today. In such a system, it would be handy to be able to jump to the next match without disturbing the search register with "_*, but with Kakoune as it is today, "_* is as useless as "_y.

1 Like

Nice to know, thank you!