2015-03-13 04:08:42 +00:00
|
|
|
# Key bindings
|
|
|
|
# ------------
|
2015-04-21 15:30:09 +00:00
|
|
|
__fzf_select__() {
|
2016-12-24 03:53:07 +00:00
|
|
|
local cmd="${FZF_CTRL_T_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/\\.*' -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
|
2016-11-14 16:51:25 +00:00
|
|
|
cmd="${FZF_ALT_C_COMMAND:-"command find -L . \\( -path '*/\\.*' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \
|
2015-11-07 15:11:46 +00:00
|
|
|
-o -type d -print 2> /dev/null | sed 1d | 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
|
|
|
}
|
|
|
|
|
2015-07-12 15:22:13 +00:00
|
|
|
__fzf_history__() (
|
2015-05-13 10:13:27 +00:00
|
|
|
local line
|
2015-07-12 15:22:13 +00:00
|
|
|
shopt -u nocaseglob nocasematch
|
2015-05-13 10:13:27 +00:00
|
|
|
line=$(
|
|
|
|
HISTTIMEFORMAT= history |
|
2017-01-15 07:15:51 +00:00
|
|
|
FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS +s --tac --no-reverse -n2..,.. --tiebreak=index --toggle-sort=ctrl-r $FZF_CTRL_R_OPTS +m" $(__fzfcmd) |
|
2016-07-06 16:40:14 +00:00
|
|
|
command grep '^ *[0-9]') &&
|
2015-07-12 15:22:13 +00:00
|
|
|
if [[ $- =~ H ]]; then
|
|
|
|
sed 's/^ *\([0-9]*\)\** .*/!\1/' <<< "$line"
|
|
|
|
else
|
|
|
|
sed 's/^ *\([0-9]*\)\** *//' <<< "$line"
|
|
|
|
fi
|
|
|
|
)
|
2015-03-13 04:08:42 +00:00
|
|
|
|
2016-05-28 17:11:50 +00:00
|
|
|
if [[ ! -o vi ]]; then
|
2015-03-13 04:08:42 +00:00
|
|
|
# Required to refresh the prompt after fzf
|
|
|
|
bind '"\er": redraw-current-line'
|
2015-05-13 10:13:27 +00:00
|
|
|
bind '"\e^": history-expand-line'
|
2015-03-13 04:08:42 +00:00
|
|
|
|
|
|
|
# CTRL-T - Paste the selected file path into the command line
|
2017-01-07 16:30:31 +00:00
|
|
|
if [ $BASH_VERSINFO -gt 3 ]; then
|
2016-04-24 05:04:15 +00:00
|
|
|
bind -x '"\C-t": "fzf-file-widget"'
|
2017-01-07 16:30:31 +00:00
|
|
|
elif __fzf_use_tmux__; then
|
2016-12-31 17:23:20 +00:00
|
|
|
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"'
|
2015-03-13 04:08:42 +00:00
|
|
|
else
|
2016-12-31 17:23:20 +00:00
|
|
|
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"'
|
2015-03-13 04:08:42 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# CTRL-R - Paste the selected command from history into the command line
|
2016-05-28 17:11:50 +00:00
|
|
|
bind '"\C-r": " \C-e\C-u`__fzf_history__`\e\C-e\e^\er"'
|
2015-03-13 04:08:42 +00:00
|
|
|
|
|
|
|
# ALT-C - cd into the selected directory
|
2016-05-28 17:11:50 +00:00
|
|
|
bind '"\ec": " \C-e\C-u`__fzf_cd__`\e\C-e\er\C-m"'
|
2015-03-13 04:08:42 +00:00
|
|
|
else
|
2016-04-12 21:20:30 +00:00
|
|
|
# We'd usually use "\e" to enter vi-movement-mode so we can do our magic,
|
|
|
|
# but this incurs a very noticeable delay of a half second or so,
|
|
|
|
# because many other commands start with "\e".
|
|
|
|
# Instead, we bind an unused key, "\C-x\C-a",
|
|
|
|
# to also enter vi-movement-mode,
|
|
|
|
# and then use that thereafter.
|
|
|
|
# (We imagine that "\C-x\C-a" is relatively unlikely to be in use.)
|
|
|
|
bind '"\C-x\C-a": vi-movement-mode'
|
|
|
|
|
2015-03-13 04:08:42 +00:00
|
|
|
bind '"\C-x\C-e": shell-expand-line'
|
|
|
|
bind '"\C-x\C-r": redraw-current-line'
|
2015-05-13 10:13:27 +00:00
|
|
|
bind '"\C-x^": history-expand-line'
|
2015-03-13 04:08:42 +00:00
|
|
|
|
|
|
|
# CTRL-T - Paste the selected file path into the command line
|
|
|
|
# - FIXME: Selected items are attached to the end regardless of cursor position
|
2017-01-07 16:30:31 +00:00
|
|
|
if [ $BASH_VERSINFO -gt 3 ]; then
|
2016-04-24 05:04:15 +00:00
|
|
|
bind -x '"\C-t": "fzf-file-widget"'
|
2017-01-07 16:30:31 +00:00
|
|
|
elif __fzf_use_tmux__; then
|
2016-12-31 17:23:20 +00:00
|
|
|
bind '"\C-t": "\C-x\C-a$a \C-x\C-addi`__fzf_select_tmux__`\C-x\C-e\C-x\C-a0P$xa"'
|
2015-03-13 04:08:42 +00:00
|
|
|
else
|
2016-12-31 17:23:20 +00:00
|
|
|
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 "'
|
2015-03-13 04:08:42 +00:00
|
|
|
fi
|
|
|
|
bind -m vi-command '"\C-t": "i\C-t"'
|
|
|
|
|
|
|
|
# CTRL-R - Paste the selected command from history into the command line
|
2016-12-31 17:23:20 +00:00
|
|
|
bind '"\C-r": "\C-x\C-addi`__fzf_history__`\C-x\C-e\C-x^\C-x\C-a$a\C-x\C-r"'
|
2015-03-13 04:08:42 +00:00
|
|
|
bind -m vi-command '"\C-r": "i\C-r"'
|
|
|
|
|
|
|
|
# ALT-C - cd into the selected directory
|
2016-12-31 17:23:20 +00:00
|
|
|
bind '"\ec": "\C-x\C-addi`__fzf_cd__`\C-x\C-e\C-x\C-r\C-m"'
|
|
|
|
bind -m vi-command '"\ec": "ddi`__fzf_cd__`\C-x\C-e\C-x\C-r\C-m"'
|
2015-03-13 04:08:42 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
fi
|