5

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.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Tomas Romero
  • 8,418
  • 11
  • 50
  • 72
  • Interesting. Thoughts: [`bind`](http://stackoverflow.com/questions/4200800) probably won't work, as I don't think you can meet the "first keystroke only" requirement. A faux command line with a high speed [read loop](http://stackoverflow.com/questions/11596059) might work. Using the terminal in [raw mode](http://unix.stackexchange.com/questions/18334/) or modifying the [bash source code](http://git.savannah.gnu.org/cgit/bash.git) are probably the most likely avenues. Also, consider [selector](http://www.idiap.ch/~fleuret/software.html#selector) instead of fzf. – bishop Jun 05 '15 at 12:38

1 Answers1

1

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
ronakg
  • 4,038
  • 21
  • 46
  • Thanks for the alternative, but I think I still prefer using ctrl-r to the arrow keys. – Tomas Romero Jun 05 '15 at 19:18
  • Sure. One advantage of this is you can search through history after you've typed few letters. `Ctrl-r` needs to be fired before typing anything. But yeah, different strokes for different folks. – ronakg Jun 05 '15 at 19:21
  • Unrelated, but I prefer `up-line-or-search` and `down-line-or-search` for up and down arrows. This has the additional advantage of "search backward/forward in the history for a line beginning with the first word in the buffer" over the default (very similar to `history-search-backward`) And I use `history-incremental-search-backward` and `history-incremental-search-forward` for `C-r` and `C-s`. – 4ae1e1 Jun 05 '15 at 21:35