From 817743df402d5a610da7a23002245075a701a123 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 22 Apr 2018 22:20:15 +0200 Subject: zshrc: Add fzf support for history Signed-off-by: Florian Pritz --- .zshrc | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/.zshrc b/.zshrc index c7dd720..48c45ba 100644 --- a/.zshrc +++ b/.zshrc @@ -194,6 +194,36 @@ globalias() { zle -N globalias bindkey ",," globalias +# Source: https://github.com/junegunn/fzf/blob/master/shell/key-bindings.zsh +__fzf_use_tmux__() { + [ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ] +} + +__fzfcmd() { + __fzf_use_tmux__ && + echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-95%}" || echo "fzf" +} + +# CTRL-R - Paste the selected command from history into the command line +# Changed to work with my personal history fetcher +fzf-history-widget() { + local selected num + setopt localoptions noglobsubst noposixbuiltins pipefail 2> /dev/null + selected=( $(cat-history-notime | + FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-95%} $FZF_DEFAULT_OPTS --tiebreak=index --bind=ctrl-r:toggle-sort $FZF_CTRL_R_OPTS --query=${(qqq)LBUFFER} +m" $(__fzfcmd)) ) + local ret=$? + if [ -n "$selected" ]; then + zle kill-buffer + BUFFER="$selected" + fi + zle redisplay + typeset -f zle-line-init >/dev/null && zle zle-line-init + return $ret +} +zle -N fzf-history-widget +bindkey '^R' fzf-history-widget + + # }}} # Variables {{{ #export CDPATH=.:$HOME @@ -358,12 +388,17 @@ cget() { curl -fJOL --compressed "$@" } -hgrep() { - mpgrep <$HISTFILE "$@" | less +cat-history() { + local histfiles=($(ls -t1 "$HISTORYDIRECTORY"/*)) + cat "${histfiles[@]}" } -hless() { - less $HISTFILE +cat-history-notime() { + cat-history "$@" | sed 's/^: [0-9]\+:0;//' +} + +hgrep() { + cat-history | mpgrep "$@" | less } # }}} -- cgit v1.2.3-24-g4f1b