2020-03-31 22:18:09 +09:00
|
|
|
# ____ ____
|
|
|
|
# / __/___ / __/
|
|
|
|
# / /_/_ / / /_
|
|
|
|
# / __/ / /_/ __/
|
|
|
|
# /_/ /___/_/ key-bindings.zsh
|
|
|
|
#
|
|
|
|
# - $FZF_TMUX_OPTS
|
|
|
|
# - $FZF_CTRL_T_COMMAND
|
|
|
|
# - $FZF_CTRL_T_OPTS
|
|
|
|
# - $FZF_CTRL_R_OPTS
|
|
|
|
# - $FZF_ALT_C_COMMAND
|
|
|
|
# - $FZF_ALT_C_OPTS
|
|
|
|
|
2015-03-13 13:08:42 +09:00
|
|
|
# Key bindings
|
|
|
|
# ------------
|
2020-03-29 18:52:48 +02:00
|
|
|
|
|
|
|
# The code at the top and the bottom of this file is the same as in completion.zsh.
|
|
|
|
# Refer to that file for explanation.
|
|
|
|
if 'zmodload' 'zsh/parameter' 2>'/dev/null' && (( ${+options} )); then
|
|
|
|
__fzf_key_bindings_options="options=(${(j: :)${(kv)options[@]}})"
|
|
|
|
else
|
|
|
|
() {
|
|
|
|
__fzf_key_bindings_options="setopt"
|
|
|
|
'local' '__fzf_opt'
|
|
|
|
for __fzf_opt in "${(@)${(@f)$(set -o)}%% *}"; do
|
|
|
|
if [[ -o "$__fzf_opt" ]]; then
|
|
|
|
__fzf_key_bindings_options+=" -o $__fzf_opt"
|
|
|
|
else
|
|
|
|
__fzf_key_bindings_options+=" +o $__fzf_opt"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
fi
|
|
|
|
|
|
|
|
'emulate' 'zsh' '-o' 'no_aliases'
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
[[ -o interactive ]] || return 0
|
2015-06-26 00:14:36 +09:00
|
|
|
|
2015-03-13 13:08:42 +09:00
|
|
|
# CTRL-T - Paste the selected file path(s) into the command line
|
|
|
|
__fsel() {
|
2017-02-19 01:33:13 +09: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 13:08:42 +09:00
|
|
|
-o -type f -print \
|
|
|
|
-o -type d -print \
|
2016-12-24 09:23:07 +05:30
|
|
|
-o -type l -print 2> /dev/null | cut -b3-"}"
|
2019-10-17 14:51:57 +09:00
|
|
|
setopt localoptions pipefail no_aliases 2> /dev/null
|
2017-01-15 16:15:51 +09:00
|
|
|
eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS" $(__fzfcmd) -m "$@" | while read item; do
|
2016-04-25 00:53:11 +09:00
|
|
|
echo -n "${(q)item} "
|
2015-03-13 13:08:42 +09:00
|
|
|
done
|
2016-06-16 01:03:48 +02:00
|
|
|
local ret=$?
|
2015-03-13 13:08:42 +09:00
|
|
|
echo
|
2016-06-16 01:03:48 +02:00
|
|
|
return $ret
|
2015-03-13 13:08:42 +09:00
|
|
|
}
|
|
|
|
|
2015-04-22 00:55:39 +09:00
|
|
|
__fzfcmd() {
|
2020-04-01 23:43:35 +09:00
|
|
|
[ -n "$TMUX_PANE" ] && { [ "${FZF_TMUX:-0}" != 0 ] || [ -n "$FZF_TMUX_OPTS" ]; } &&
|
2020-03-31 22:18:09 +09:00
|
|
|
echo "fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- " || echo "fzf"
|
2015-04-22 00:55:39 +09:00
|
|
|
}
|
|
|
|
|
2015-04-22 00:30:09 +09:00
|
|
|
fzf-file-widget() {
|
|
|
|
LBUFFER="${LBUFFER}$(__fsel)"
|
2016-06-16 01:03:48 +02:00
|
|
|
local ret=$?
|
2018-10-05 03:56:26 +02:00
|
|
|
zle reset-prompt
|
2016-06-16 01:03:48 +02:00
|
|
|
return $ret
|
2015-04-22 00:30:09 +09:00
|
|
|
}
|
2015-03-13 13:08:42 +09:00
|
|
|
zle -N fzf-file-widget
|
|
|
|
bindkey '^T' fzf-file-widget
|
|
|
|
|
2017-11-13 19:43:52 -08:00
|
|
|
# Ensure precmds are run after cd
|
|
|
|
fzf-redraw-prompt() {
|
|
|
|
local precmd
|
|
|
|
for precmd in $precmd_functions; do
|
|
|
|
$precmd
|
|
|
|
done
|
|
|
|
zle reset-prompt
|
|
|
|
}
|
|
|
|
zle -N fzf-redraw-prompt
|
|
|
|
|
2015-03-13 13:08:42 +09:00
|
|
|
# ALT-C - cd into the selected directory
|
|
|
|
fzf-cd-widget() {
|
2017-02-19 01:33:13 +09:00
|
|
|
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 \
|
2017-02-16 09:18:01 +01:00
|
|
|
-o -type d -print 2> /dev/null | cut -b3-"}"
|
2019-10-17 14:51:57 +09:00
|
|
|
setopt localoptions pipefail no_aliases 2> /dev/null
|
2017-02-08 23:05:02 -06:00
|
|
|
local dir="$(eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_ALT_C_OPTS" $(__fzfcmd) +m)"
|
|
|
|
if [[ -z "$dir" ]]; then
|
|
|
|
zle redisplay
|
|
|
|
return 0
|
|
|
|
fi
|
2020-10-03 14:48:57 +09:00
|
|
|
if [ -z "$BUFFER" ]; then
|
|
|
|
BUFFER="cd ${(q)dir}"
|
|
|
|
zle accept-line
|
|
|
|
else
|
|
|
|
print -sr "cd ${(q)dir}"
|
|
|
|
cd "$dir"
|
|
|
|
fi
|
2016-06-16 01:03:48 +02:00
|
|
|
local ret=$?
|
2020-10-03 14:48:57 +09:00
|
|
|
unset dir # ensure this doesn't end up appearing in prompt expansion
|
2017-11-13 19:43:52 -08:00
|
|
|
zle fzf-redraw-prompt
|
2016-06-16 01:03:48 +02:00
|
|
|
return $ret
|
2015-03-13 13:08:42 +09:00
|
|
|
}
|
|
|
|
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() {
|
2015-11-04 21:55:35 +09:00
|
|
|
local selected num
|
2019-10-17 14:51:57 +09:00
|
|
|
setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases 2> /dev/null
|
2020-09-02 17:27:56 +09:00
|
|
|
selected=( $(fc -rl 1 | perl -ne 'print if !$seen{(/^\s*[0-9]+\**\s+(.*)/, $1)}++' |
|
2017-10-20 05:56:02 +02:00
|
|
|
FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort $FZF_CTRL_R_OPTS --query=${(qqq)LBUFFER} +m" $(__fzfcmd)) )
|
2016-06-16 01:03:48 +02:00
|
|
|
local ret=$?
|
2015-11-03 17:52:44 -08:00
|
|
|
if [ -n "$selected" ]; then
|
2015-06-21 00:44:27 +02:00
|
|
|
num=$selected[1]
|
2015-04-23 22:39:07 +09:00
|
|
|
if [ -n "$num" ]; then
|
2015-06-21 00:44:27 +02:00
|
|
|
zle vi-fetch-history -n $num
|
2015-04-23 22:31:23 +09:00
|
|
|
fi
|
2015-03-13 13:08:42 +09:00
|
|
|
fi
|
2018-10-05 03:56:26 +02:00
|
|
|
zle reset-prompt
|
2016-06-16 01:03:48 +02:00
|
|
|
return $ret
|
2015-03-13 13:08:42 +09:00
|
|
|
}
|
|
|
|
zle -N fzf-history-widget
|
|
|
|
bindkey '^R' fzf-history-widget
|
|
|
|
|
2020-03-29 18:52:48 +02:00
|
|
|
} always {
|
|
|
|
eval $__fzf_key_bindings_options
|
|
|
|
'unset' '__fzf_key_bindings_options'
|
|
|
|
}
|