Jander
Jander

Reputation: 5637

How do you add to the Vim search history without opening a window?

I am writing a function that acts like the * key but does not move the cursor. Setting the current search pattern is easy:

let @/='...'

so n and N and even // work as expected. But * also adds an entry to the search history. The best I've come up with is:

silent! normal! q/"/p

but that opens the search history window briefly, which causes ugly flickering when I trigger the function. Is there a better way?

Upvotes: 2

Views: 328

Answers (2)

joharr
joharr

Reputation: 535

:call histadd('/', 'foo') then check your search history.

Upvotes: 1

mMontu
mMontu

Reputation: 9273

Check :help histadd()

histadd({history}, {item})              *histadd()*
        Add the String {item} to the history {history} which can be
        one of:                 *hist-names*
            "cmd"    or ":"   command line history
            "search" or "/"   search pattern history
            "expr"   or "="   typed expression history
            "input"  or "@"   input line history
            "debug"  or ">"   debug command history
        The {history} string does not need to be the whole name, one
        character is sufficient.
        If {item} does already exist in the history, it will be
        shifted to become the newest entry.
        The result is a Number: 1 if the operation was successful,
        otherwise 0 is returned.

When writing vimscript the info from :help function-list can be very helpful (the entire chapter 41, actually).

Upvotes: 5

Related Questions