Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 421
Tomas Romero Avatar asked Nov 09 '22 12:11

Tomas Romero


1 Answers

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
like image 134
ronakg Avatar answered Nov 15 '22 06:11

ronakg