Tomas Romero
Tomas Romero

Reputation: 8708

How to run a function on first keystroke in bash/zsh?

I want to run a function when I start typing in the terminal.

Specifically I want to run the fzf function, for which I currently need to press ctrl-r to trigger it. I would like any keystroke to trigger it so history always appears when I type.

Only the first keystroke should run the function, because running it multiple time toggles between path and filename selection.

Upvotes: 5

Views: 436

Answers (1)

ronakg
ronakg

Reputation: 4212

I have bound the Up and Down arrow keys to history-search-backward and history-search-forward respectively. So when I type something and then press Up or Down, it does a history search starting with typed letters. This works for me as I don't want to do a history search for every command entered.

I know this isn't exactly what you're looking for, but it's close.

# Bind up and down arrows to do backward and forward history search
if [[ $- == *i* ]]
then
    bind '"\e[A": history-search-backward'
    bind '"\e[B": history-search-forward'
fi

Upvotes: 1

Related Questions