|
| 1 | +# Key bindings |
| 2 | +# ------------ |
| 3 | +__fzf_select__() { |
| 4 | + 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 \ |
| 5 | + -o -type f -print \ |
| 6 | + -o -type d -print \ |
| 7 | + -o -type l -print 2> /dev/null | cut -b3-"}" |
| 8 | + eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS" fzf -m "$@" | while read -r item; do |
| 9 | + printf '%q ' "$item" |
| 10 | + done |
| 11 | + echo |
| 12 | +} |
| 13 | + |
| 14 | +if [[ $- =~ i ]]; then |
| 15 | + |
| 16 | +__fzf_use_tmux__() { |
| 17 | + [ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ] |
| 18 | +} |
| 19 | + |
| 20 | +__fzfcmd() { |
| 21 | + __fzf_use_tmux__ && |
| 22 | + echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf" |
| 23 | +} |
| 24 | + |
| 25 | +__fzf_select_tmux__() { |
| 26 | + local height |
| 27 | + height=${FZF_TMUX_HEIGHT:-40%} |
| 28 | + if [[ $height =~ %$ ]]; then |
| 29 | + height="-p ${height%\%}" |
| 30 | + else |
| 31 | + height="-l $height" |
| 32 | + fi |
| 33 | + |
| 34 | + tmux split-window $height "cd $(printf %q "$PWD"); FZF_DEFAULT_OPTS=$(printf %q "$FZF_DEFAULT_OPTS") PATH=$(printf %q "$PATH") FZF_CTRL_T_COMMAND=$(printf %q "$FZF_CTRL_T_COMMAND") FZF_CTRL_T_OPTS=$(printf %q "$FZF_CTRL_T_OPTS") bash -c 'source \"${BASH_SOURCE[0]}\"; RESULT=\"\$(__fzf_select__ --no-height)\"; tmux setb -b fzf \"\$RESULT\" \\; pasteb -b fzf -t $TMUX_PANE \\; deleteb -b fzf || tmux send-keys -t $TMUX_PANE \"\$RESULT\"'" |
| 35 | +} |
| 36 | + |
| 37 | +fzf-file-widget() { |
| 38 | + if __fzf_use_tmux__; then |
| 39 | + __fzf_select_tmux__ |
| 40 | + else |
| 41 | + local selected="$(__fzf_select__)" |
| 42 | + READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$selected${READLINE_LINE:$READLINE_POINT}" |
| 43 | + READLINE_POINT=$(( READLINE_POINT + ${#selected} )) |
| 44 | + fi |
| 45 | +} |
| 46 | + |
| 47 | +__fzf_cd__() { |
| 48 | + local cmd dir |
| 49 | + cmd="${FZF_ALT_C_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \ |
| 50 | + -o -type d -print 2> /dev/null | cut -b3-"}" |
| 51 | + dir=$(eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_ALT_C_OPTS" $(__fzfcmd) +m) && printf 'cd %q' "$dir" |
| 52 | +} |
| 53 | + |
| 54 | +__fzf_history__() ( |
| 55 | + local line |
| 56 | + shopt -u nocaseglob nocasematch |
| 57 | + line=$( |
| 58 | + HISTTIMEFORMAT= builtin history | |
| 59 | + FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS --tac --sync -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort $FZF_CTRL_R_OPTS +m" $(__fzfcmd) | |
| 60 | + command grep '^ *[0-9]') && |
| 61 | + if [[ $- =~ H ]]; then |
| 62 | + sed 's/^ *\([0-9]*\)\** .*/!\1/' <<< "$line" |
| 63 | + else |
| 64 | + sed 's/^ *\([0-9]*\)\** *//' <<< "$line" |
| 65 | + fi |
| 66 | +) |
| 67 | + |
| 68 | +if [[ ! -o vi ]]; then |
| 69 | + # Required to refresh the prompt after fzf |
| 70 | + bind '"\er": redraw-current-line' |
| 71 | + bind '"\e^": history-expand-line' |
| 72 | + |
| 73 | + # CTRL-T - Paste the selected file path into the command line |
| 74 | + if [ $BASH_VERSINFO -gt 3 ]; then |
| 75 | + bind -x '"\C-t": "fzf-file-widget"' |
| 76 | + elif __fzf_use_tmux__; then |
| 77 | + bind '"\C-t": " \C-u \C-a\C-k`__fzf_select_tmux__`\e\C-e\C-y\C-a\C-d\C-y\ey\C-h"' |
| 78 | + else |
| 79 | + bind '"\C-t": " \C-u \C-a\C-k`__fzf_select__`\e\C-e\C-y\C-a\C-y\ey\C-h\C-e\er \C-h"' |
| 80 | + fi |
| 81 | + |
| 82 | + # CTRL-R - Paste the selected command from history into the command line |
| 83 | + bind '"\C-xr": " \C-e\C-u\C-y\ey\C-u`__fzf_history__`\e\C-e\er\e^"' |
| 84 | + |
| 85 | + # ALT-C - cd into the selected directory |
| 86 | + bind '"\ec": " \C-e\C-u`__fzf_cd__`\e\C-e\er\C-m"' |
| 87 | +else |
| 88 | + # We'd usually use "\e" to enter vi-movement-mode so we can do our magic, |
| 89 | + # but this incurs a very noticeable delay of a half second or so, |
| 90 | + # because many other commands start with "\e". |
| 91 | + # Instead, we bind an unused key, "\C-x\C-a", |
| 92 | + # to also enter vi-movement-mode, |
| 93 | + # and then use that thereafter. |
| 94 | + # (We imagine that "\C-x\C-a" is relatively unlikely to be in use.) |
| 95 | + bind '"\C-x\C-a": vi-movement-mode' |
| 96 | + |
| 97 | + bind '"\C-x\C-e": shell-expand-line' |
| 98 | + bind '"\C-x\C-r": redraw-current-line' |
| 99 | + bind '"\C-x^": history-expand-line' |
| 100 | + |
| 101 | + # CTRL-T - Paste the selected file path into the command line |
| 102 | + # - FIXME: Selected items are attached to the end regardless of cursor position |
| 103 | + if [ $BASH_VERSINFO -gt 3 ]; then |
| 104 | + bind -x '"\C-t": "fzf-file-widget"' |
| 105 | + elif __fzf_use_tmux__; then |
| 106 | + bind '"\C-t": "\C-x\C-a$a \C-x\C-addi`__fzf_select_tmux__`\C-x\C-e\C-x\C-a0P$xa"' |
| 107 | + else |
| 108 | + bind '"\C-t": "\C-x\C-a$a \C-x\C-addi`__fzf_select__`\C-x\C-e\C-x\C-a0Px$a \C-x\C-r\C-x\C-axa "' |
| 109 | + fi |
| 110 | + bind -m vi-command '"\C-t": "i\C-t"' |
| 111 | + |
| 112 | + # CTRL-R - Paste the selected command from history into the command line |
| 113 | + bind '"\C-r": "\C-x\C-addi`__fzf_history__`\C-x\C-e\C-x\C-r\C-x^\C-x\C-a$a"' |
| 114 | + bind -m vi-command '"\C-r": "i\C-r"' |
| 115 | + |
| 116 | + # ALT-C - cd into the selected directory |
| 117 | + bind '"\ec": "\C-x\C-addi`__fzf_cd__`\C-x\C-e\C-x\C-r\C-m"' |
| 118 | + bind -m vi-command '"\ec": "ddi`__fzf_cd__`\C-x\C-e\C-x\C-r\C-m"' |
| 119 | +fi |
| 120 | + |
| 121 | +fi |
0 commit comments