From f7386c50e4c2d2330c7d4fe3c0992911e01be971 Mon Sep 17 00:00:00 2001 From: Samir Benmendil Date: Wed, 8 Apr 2015 14:18:56 +0100 Subject: [PATCH] zsh: load fzf if installed --- zsh/lib/fzf.zsh | 81 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100755 zsh/lib/fzf.zsh diff --git a/zsh/lib/fzf.zsh b/zsh/lib/fzf.zsh new file mode 100755 index 0000000..7341504 --- /dev/null +++ b/zsh/lib/fzf.zsh @@ -0,0 +1,81 @@ +if [[ ! -x /usr/bin/fzf ]]; then + echo '`fzf` is not installed.' + return +fi + +# Auto-completion +# --------------- +if [[ -f /usr/share/zsh/site-functions/_fzf ]]; then + source /usr/share/zsh/site-functions/_fzf +fi + +# Key bindings +# ------------ + +# CTRL-T - Paste the selected file path(s) into the command line +__fsel() { + command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \ + -o -type f -print \ + -o -type d -print \ + -o -type l -print 2> /dev/null | sed 1d | cut -b3- | fzf -m | while read item; do + printf '%q ' "$item" + done + echo +} + +if [[ $- =~ i ]]; then + +if [ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ]; then + fzf-file-widget() { + local height + height=${FZF_TMUX_HEIGHT:-40%} + if [[ $height =~ %$ ]]; then + height="-p ${height%\%}" + else + height="-l $height" + fi + tmux split-window $height "cd $(printf %q "$PWD");zsh -c 'tmux send-keys -t $TMUX_PANE \"\$(__fsel)\"'" + } +else + fzf-file-widget() { + LBUFFER="${LBUFFER}$(__fsel)" + zle redisplay + } +fi +zle -N fzf-file-widget +bindkey '^T' fzf-file-widget + +# ALT-C - cd into the selected directory +fzf-cd-widget() { + cd "${$(command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \ + -o -type d -print 2> /dev/null | sed 1d | cut -b3- | fzf +m):-.}" + zle reset-prompt +} +zle -N fzf-cd-widget +bindkey '\ec' fzf-cd-widget + +# CTRL-R - Paste the selected command from history into the command line +fzf-history-widget() { + local selected + if selected=$(fc -l 1 | fzf -x +s --tac +m -n2..,.. -q "$LBUFFER"); then + num=$(echo "$selected" | head -1 | awk '{print $1}' | sed 's/[^0-9]//g') + LBUFFER=!$num + zle expand-history + fi + zle redisplay +} +zle -N fzf-history-widget +bindkey '^R' fzf-history-widget + +# ALT-I - Paste the selected entry from locate output into the command line +fzf-locate-widget() { + local selected + if selected=$(locate / | fzf -q "$LBUFFER"); then + LBUFFER=$selected + fi + zle redisplay +} +zle -N fzf-locate-widget +bindkey '\ei' fzf-locate-widget + +fi -- 2.48.1