From 72077fa610ff21f7ca4e438df86b5f746d6267e3 Mon Sep 17 00:00:00 2001 From: Nadim Khemir Date: Tue, 4 Oct 2016 12:50:17 +0200 Subject: [PATCH] re-wrote and simplified history search script --- Examples.md | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/Examples.md b/Examples.md index b6b1d76..1fb3b16 100644 --- a/Examples.md +++ b/Examples.md @@ -173,11 +173,11 @@ fh() { } ``` -Replacing `eval` with `print -z` will push the arguments onto the editing buffer stack, allowing you to edit the command before running it. It also means the command you run will appear in your history rather than just `fh`. Unfortunately this only works for zsh. +Replacing `eval` with `print -z` will push the arguments onto the editing buffer stack, allowing you to edit the command before running it. It also means the command you run will appear in your history rather than just `fh`. Unfortunately this only works for zsh. See below for solutions working with Bash. #### With write to terminal capabilities -These have been tested in bash. +These have been tested in bash. ```sh # fh - repeat history @@ -234,6 +234,32 @@ builtin bind -x '"\C-x1": __fzf_history'; builtin bind '"\C-r": "\C-x1\e^\er"' ``` +```sh +# re-wrote the script above +bind '"\C-r": "\C-x1\e^\er"' +bind -x '"\C-x1": __fzf_history'; + +__fzf_history () +{ +__ehc $(history | fzf --tac --tiebreak=index | perl -ne 'm/^\s*([0-9]+)/ and print "!$1"') +} + +__ehc() +{ +if + [[ -n $1 ]] +then + bind '"\er": redraw-current-line' + bind '"\e^": magic-space' + READLINE_LINE=${READLINE_LINE:+${READLINE_LINE:0:READLINE_POINT}}${1}${READLINE_LINE:+${READLINE_LINE:READLINE_POINT}} + READLINE_POINT=$(( READLINE_POINT + ${#1} )) +else + bind '"\er":' + bind '"\e^":' +fi +} +``` + ### Toggle options ```sh