2015-03-13 04:08:42 +00:00
|
|
|
# Key bindings
|
|
|
|
# ------------
|
2015-04-21 15:30:09 +00:00
|
|
|
__fzf_select__() {
|
2017-02-18 16:33:13 +00:00
|
|
|
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 \
|
2015-03-13 04:08:42 +00:00
|
|
|
-o -type f -print \
|
|
|
|
-o -type d -print \
|
2016-12-24 03:53:07 +00:00
|
|
|
-o -type l -print 2> /dev/null | cut -b3-"}"
|
2017-01-15 07:15:51 +00:00
|
|
|
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
|
2015-03-13 04:08:42 +00:00
|
|
|
printf '%q ' "$item"
|
|
|
|
done
|
|
|
|
echo
|
|
|
|
}
|
|
|
|
|
|
|
|
if [[ $- =~ i ]]; then
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
__fzf_use_tmux__() {
|
|
|
|
[ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ]
|
|
|
|
}
|
|
|
|
|
2015-04-21 15:55:39 +00:00
|
|
|
__fzfcmd() {
|
2017-01-07 16:30:31 +00:00
|
|
|
__fzf_use_tmux__ &&
|
2017-01-15 07:15:51 +00:00
|
|
|
echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf"
|
2015-04-21 15:55:39 +00:00
|
|
|
}
|
|
|
|
|
2015-04-21 15:30:09 +00:00
|
|
|
__fzf_select_tmux__() {
|
2015-03-13 04:08:42 +00:00
|
|
|
local height
|
|
|
|
height=${FZF_TMUX_HEIGHT:-40%}
|
|
|
|
if [[ $height =~ %$ ]]; then
|
|
|
|
height="-p ${height%\%}"
|
|
|
|
else
|
|
|
|
height="-l $height"
|
|
|
|
fi
|
2015-12-25 20:05:25 +00:00
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
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\"'"
|
2015-03-13 04:08:42 +00:00
|
|
|
}
|
|
|
|
|
2016-04-24 05:04:15 +00:00
|
|
|
fzf-file-widget() {
|
|
|
|
if __fzf_use_tmux__; then
|
2015-12-11 03:32:19 +00:00
|
|
|
__fzf_select_tmux__
|
|
|
|
else
|
2017-01-15 07:15:51 +00:00
|
|
|
local selected="$(__fzf_select__)"
|
2016-04-24 05:04:15 +00:00
|
|
|
READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$selected${READLINE_LINE:$READLINE_POINT}"
|
|
|
|
READLINE_POINT=$(( READLINE_POINT + ${#selected} ))
|
2015-12-11 03:32:19 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-04-21 15:30:09 +00:00
|
|
|
__fzf_cd__() {
|
2015-11-07 15:11:46 +00:00
|
|
|
local cmd dir
|
2017-02-18 16:33:13 +00:00
|
|
|
cmd="${FZF_ALT_C_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \
|
2017-02-16 08:18:01 +00:00
|
|
|
-o -type d -print 2> /dev/null | cut -b3-"}"
|
2017-01-15 07:15:51 +00:00
|
|
|
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"
|
2015-04-21 15:30:09 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 09:47:13 +00:00
|
|
|
__fzf_history__() {
|
|
|
|
local output
|
|
|
|
output=$(
|
2020-02-23 15:17:38 +00:00
|
|
|
builtin fc -lnr -2147483648 |
|
2020-03-29 12:26:52 +00:00
|
|
|
last_hist=$(HISTTIMEFORMAT='' builtin history 1) perl -n -l0 -e 'BEGIN { getc; $/ = "\n\t"; $HISTCMD = $ENV{last_hist} + 1 } s/^[ *]//; print $HISTCMD - $. . "\t$_" if !$seen{$_}++' |
|
2020-03-13 00:13:38 +00:00
|
|
|
FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort $FZF_CTRL_R_OPTS +m --read0" $(__fzfcmd) --query "$READLINE_LINE"
|
2020-02-28 09:47:13 +00:00
|
|
|
) || return
|
|
|
|
READLINE_LINE=${output#*$'\t'}
|
|
|
|
if [ -z "$READLINE_POINT" ]; then
|
|
|
|
echo "$READLINE_LINE"
|
|
|
|
else
|
|
|
|
READLINE_POINT=0x7fffffff
|
|
|
|
fi
|
|
|
|
}
|
2015-03-13 04:08:42 +00:00
|
|
|
|
2020-01-08 09:35:43 +00:00
|
|
|
# Required to refresh the prompt after fzf
|
|
|
|
bind -m emacs-standard '"\er": redraw-current-line'
|
|
|
|
|
2020-02-21 00:51:34 +00:00
|
|
|
bind -m vi-command '"\C-z": emacs-editing-mode'
|
|
|
|
bind -m vi-insert '"\C-z": emacs-editing-mode'
|
|
|
|
bind -m emacs-standard '"\C-z": vi-editing-mode'
|
2020-01-08 09:35:43 +00:00
|
|
|
|
2020-02-28 09:47:13 +00:00
|
|
|
if [ "${BASH_VERSINFO[0]}" -lt 4 ]; then
|
|
|
|
# CTRL-T - Paste the selected file path into the command line
|
|
|
|
if __fzf_use_tmux__; then
|
|
|
|
bind -m emacs-standard '"\C-t": " \C-b\C-k \C-u`__fzf_select_tmux__`\e\C-e\C-a\C-y\C-h\C-e\e \C-y\ey\C-x\C-x\C-f"'
|
|
|
|
else
|
|
|
|
bind -m emacs-standard '"\C-t": " \C-b\C-k \C-u`__fzf_select__`\e\C-e\er\C-a\C-y\C-h\C-e\e \C-y\ey\C-x\C-x\C-f"'
|
|
|
|
fi
|
|
|
|
bind -m vi-command '"\C-t": "\C-z\C-t\C-z"'
|
|
|
|
bind -m vi-insert '"\C-t": "\C-z\C-t\C-z"'
|
2015-03-13 04:08:42 +00:00
|
|
|
|
2020-02-28 09:47:13 +00:00
|
|
|
# CTRL-R - Paste the selected command from history into the command line
|
|
|
|
bind -m emacs-standard '"\C-r": "\C-e \C-u\C-y\ey\C-u"$(__fzf_history__)"\e\C-e\er"'
|
|
|
|
bind -m vi-command '"\C-r": "\C-z\C-r\C-z"'
|
|
|
|
bind -m vi-insert '"\C-r": "\C-z\C-r\C-z"'
|
|
|
|
else
|
|
|
|
# CTRL-T - Paste the selected file path into the command line
|
|
|
|
bind -m emacs-standard -x '"\C-t": fzf-file-widget'
|
|
|
|
bind -m vi-command -x '"\C-t": fzf-file-widget'
|
|
|
|
bind -m vi-insert -x '"\C-t": fzf-file-widget'
|
|
|
|
|
|
|
|
# CTRL-R - Paste the selected command from history into the command line
|
|
|
|
bind -m emacs-standard -x '"\C-r": __fzf_history__'
|
|
|
|
bind -m vi-command -x '"\C-r": __fzf_history__'
|
|
|
|
bind -m vi-insert -x '"\C-r": __fzf_history__'
|
|
|
|
fi
|
2015-03-13 04:08:42 +00:00
|
|
|
|
2020-01-08 09:35:43 +00:00
|
|
|
# ALT-C - cd into the selected directory
|
2020-02-28 09:47:13 +00:00
|
|
|
bind -m emacs-standard '"\ec": " \C-b\C-k \C-u`__fzf_cd__`\e\C-e\er\C-m\C-y\C-h\e \C-y\ey\C-x\C-x\C-d"'
|
2020-02-21 00:51:34 +00:00
|
|
|
bind -m vi-command '"\ec": "\C-z\ec\C-z"'
|
|
|
|
bind -m vi-insert '"\ec": "\C-z\ec\C-z"'
|
2015-03-13 04:08:42 +00:00
|
|
|
|
|
|
|
fi
|