/ scripts / fzf.zsh
fzf.zsh
  1  #     ____      ____
  2  #    / __/___  / __/
  3  #   / /_/_  / / /_
  4  #  / __/ / /_/ __/
  5  # /_/   /___/_/ key-bindings.zsh
  6  #
  7  # - $FZF_TMUX_OPTS
  8  # - $FZF_CTRL_T_COMMAND
  9  # - $FZF_CTRL_T_OPTS
 10  # - $FZF_CTRL_R_OPTS
 11  # - $FZF_ALT_C_COMMAND
 12  # - $FZF_ALT_C_OPTS
 13  
 14  # Key bindings
 15  # ------------
 16  
 17  # The code at the top and the bottom of this file is the same as in completion.zsh.
 18  # Refer to that file for explanation.
 19  if 'zmodload' 'zsh/parameter' 2>'/dev/null' && (( ${+options} )); then
 20      __fzf_key_bindings_options="options=(${(j: :)${(kv)options[@]}})"
 21  else
 22      () {
 23          __fzf_key_bindings_options="setopt"
 24          'local' '__fzf_opt'
 25          for __fzf_opt in "${(@)${(@f)$(set -o)}%% *}"; do
 26              if [[ -o "$__fzf_opt" ]]; then
 27                  __fzf_key_bindings_options+=" -o $__fzf_opt"
 28              else
 29                  __fzf_key_bindings_options+=" +o $__fzf_opt"
 30              fi
 31          done
 32      }
 33  fi
 34  
 35  'emulate' 'zsh' '-o' 'no_aliases'
 36  
 37  {
 38  
 39      [[ -o interactive ]] || return 0
 40  
 41      # CTRL-T - Paste the selected file path(s) into the command line
 42      __fsel() {
 43          local cmd="${FZF_CTRL_T_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \
 44              -o -type f -print \
 45              -o -type d -print \
 46              -o -type l -print 2> /dev/null | cut -b3-"}"
 47          setopt localoptions pipefail no_aliases 2> /dev/null
 48          local item
 49          eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS" $(__fzfcmd) -m "$@" | while read item; do
 50              echo -n "${(q)item} "
 51          done
 52          local ret=$?
 53          echo
 54          return $ret
 55      }
 56  
 57      __fzfcmd() {
 58          [ -n "$TMUX_PANE" ] && { [ "${FZF_TMUX:-0}" != 0 ] || [ -n "$FZF_TMUX_OPTS" ]; } &&
 59          echo "fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- " || echo "fzf"
 60      }
 61  
 62      fzf-file-widget() {
 63          LBUFFER="${LBUFFER}$(__fsel)"
 64          local ret=$?
 65          zle reset-prompt
 66          return $ret
 67      }
 68      zle     -N   fzf-file-widget
 69      bindkey '^T' fzf-file-widget
 70  
 71      # ALT-C - cd into the selected directory
 72      fzf-cd-widget() {
 73          local cmd="${FZF_ALT_C_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \
 74              -o -type d -print 2> /dev/null | cut -b3-"}"
 75          setopt localoptions pipefail no_aliases 2> /dev/null
 76          local dir="$(eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore $FZF_DEFAULT_OPTS $FZF_ALT_C_OPTS" $(__fzfcmd) +m)"
 77          if [[ -z "$dir" ]]; then
 78              zle redisplay
 79              return 0
 80          fi
 81          zle push-line # Clear buffer. Auto-restored on next prompt.
 82          BUFFER="cd ${(q)dir}"
 83          zle accept-line
 84          local ret=$?
 85          unset dir # ensure this doesn't end up appearing in prompt expansion
 86          zle reset-prompt
 87          return $ret
 88      }
 89      zle     -N    fzf-cd-widget
 90      bindkey '\ec' fzf-cd-widget
 91  
 92      # CTRL-R - Paste the selected command from history into the command line
 93      fzf-history-widget() {
 94          local selected num
 95          setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases 2> /dev/null
 96          selected=( $(fc -rl 1 | perl -ne 'print if !$seen{(/^\s*[0-9]+\**\s+(.*)/, $1)}++' |
 97          FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort,ctrl-z:ignore $FZF_CTRL_R_OPTS --query=${(qqq)LBUFFER} +m" $(__fzfcmd)) )
 98          local ret=$?
 99          if [ -n "$selected" ]; then
100              num=$selected[1]
101              if [ -n "$num" ]; then
102                  zle vi-fetch-history -n $num
103              fi
104          fi
105          zle reset-prompt
106          return $ret
107      }
108      zle     -N   fzf-history-widget
109      bindkey '^R' fzf-history-widget
110  
111      } always {
112      eval $__fzf_key_bindings_options
113      'unset' '__fzf_key_bindings_options'
114  }