2020-03-31 13:18:09 +00: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
|
|
|
|
|
2023-10-08 16:19:28 +00:00
|
|
|
|
2015-03-13 04:08:42 +00:00
|
|
|
# Key bindings
|
|
|
|
# ------------
|
2020-03-29 16:52:48 +00: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
|
|
|
|
|
2023-10-14 08:41:01 +00:00
|
|
|
'builtin' 'emulate' 'zsh' && 'builtin' 'setopt' 'no_aliases'
|
2020-03-29 16:52:48 +00:00
|
|
|
|
|
|
|
{
|
2024-04-17 09:03:12 +00:00
|
|
|
if [[ -o interactive ]]; then
|
2020-03-29 16:52:48 +00:00
|
|
|
|
2024-04-19 13:40:38 +00:00
|
|
|
__fzf_defaults() {
|
|
|
|
# $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
|
|
|
|
# $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
|
|
|
|
echo "--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore $1"
|
|
|
|
command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null
|
|
|
|
echo "${FZF_DEFAULT_OPTS-} $2"
|
|
|
|
}
|
|
|
|
|
2015-03-13 04:08:42 +00:00
|
|
|
# CTRL-T - Paste the selected file path(s) into the command line
|
2024-04-19 13:40:38 +00:00
|
|
|
__fzf_select() {
|
2019-10-17 05:51:57 +00:00
|
|
|
setopt localoptions pipefail no_aliases 2> /dev/null
|
2020-11-25 02:21:30 +00:00
|
|
|
local item
|
2024-04-19 13:40:38 +00:00
|
|
|
FZF_DEFAULT_COMMAND=${FZF_CTRL_T_COMMAND:-} \
|
|
|
|
FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --walker=file,dir,follow,hidden --scheme=path" "${FZF_CTRL_T_OPTS-} -m") \
|
|
|
|
FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) "$@" < /dev/tty | while read item; do
|
2016-04-24 15:53:11 +00:00
|
|
|
echo -n "${(q)item} "
|
2015-03-13 04:08:42 +00:00
|
|
|
done
|
2016-06-15 23:03:48 +00:00
|
|
|
local ret=$?
|
2015-03-13 04:08:42 +00:00
|
|
|
echo
|
2016-06-15 23:03:48 +00:00
|
|
|
return $ret
|
2015-03-13 04:08:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-21 15:55:39 +00:00
|
|
|
__fzfcmd() {
|
2022-10-16 08:15:19 +00:00
|
|
|
[ -n "${TMUX_PANE-}" ] && { [ "${FZF_TMUX:-0}" != 0 ] || [ -n "${FZF_TMUX_OPTS-}" ]; } &&
|
2020-03-31 13:18:09 +00:00
|
|
|
echo "fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- " || echo "fzf"
|
2015-04-21 15:55:39 +00:00
|
|
|
}
|
|
|
|
|
2015-04-21 15:30:09 +00:00
|
|
|
fzf-file-widget() {
|
2024-04-19 13:40:38 +00:00
|
|
|
LBUFFER="${LBUFFER}$(__fzf_select)"
|
2016-06-15 23:03:48 +00:00
|
|
|
local ret=$?
|
2018-10-05 01:56:26 +00:00
|
|
|
zle reset-prompt
|
2016-06-15 23:03:48 +00:00
|
|
|
return $ret
|
2015-04-21 15:30:09 +00:00
|
|
|
}
|
2024-03-17 07:06:48 +00:00
|
|
|
if [[ "${FZF_CTRL_T_COMMAND-x}" != "" ]]; then
|
|
|
|
zle -N fzf-file-widget
|
|
|
|
bindkey -M emacs '^T' fzf-file-widget
|
|
|
|
bindkey -M vicmd '^T' fzf-file-widget
|
|
|
|
bindkey -M viins '^T' fzf-file-widget
|
|
|
|
fi
|
2015-03-13 04:08:42 +00:00
|
|
|
|
|
|
|
# ALT-C - cd into the selected directory
|
|
|
|
fzf-cd-widget() {
|
2019-10-17 05:51:57 +00:00
|
|
|
setopt localoptions pipefail no_aliases 2> /dev/null
|
2024-04-19 13:40:38 +00:00
|
|
|
local dir="$(
|
|
|
|
FZF_DEFAULT_COMMAND=${FZF_ALT_C_COMMAND:-} \
|
|
|
|
FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --walker=dir,follow,hidden --scheme=path" "${FZF_ALT_C_OPTS-} +m") \
|
|
|
|
FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) < /dev/tty)"
|
2017-02-09 05:05:02 +00:00
|
|
|
if [[ -z "$dir" ]]; then
|
|
|
|
zle redisplay
|
|
|
|
return 0
|
|
|
|
fi
|
2021-02-03 10:26:17 +00:00
|
|
|
zle push-line # Clear buffer. Auto-restored on next prompt.
|
2024-03-30 16:13:15 +00:00
|
|
|
BUFFER="builtin cd -- ${(q)dir:a}"
|
2021-02-03 10:26:17 +00:00
|
|
|
zle accept-line
|
2016-06-15 23:03:48 +00:00
|
|
|
local ret=$?
|
2020-10-03 05:48:57 +00:00
|
|
|
unset dir # ensure this doesn't end up appearing in prompt expansion
|
2021-02-03 10:26:17 +00:00
|
|
|
zle reset-prompt
|
2016-06-15 23:03:48 +00:00
|
|
|
return $ret
|
2015-03-13 04:08:42 +00:00
|
|
|
}
|
2024-03-17 07:06:48 +00:00
|
|
|
if [[ "${FZF_ALT_C_COMMAND-x}" != "" ]]; then
|
|
|
|
zle -N fzf-cd-widget
|
|
|
|
bindkey -M emacs '\ec' fzf-cd-widget
|
|
|
|
bindkey -M vicmd '\ec' fzf-cd-widget
|
|
|
|
bindkey -M viins '\ec' fzf-cd-widget
|
|
|
|
fi
|
2015-03-13 04:08:42 +00:00
|
|
|
|
|
|
|
# CTRL-R - Paste the selected command from history into the command line
|
|
|
|
fzf-history-widget() {
|
2015-11-04 12:55:35 +00:00
|
|
|
local selected num
|
2024-06-13 01:43:33 +00:00
|
|
|
setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases noglob 2> /dev/null
|
2024-05-31 01:43:58 +00:00
|
|
|
# Ensure the associative history array, which maps event numbers to the full
|
|
|
|
# history lines, is loaded, and that Perl is installed for multi-line output.
|
|
|
|
if zmodload -F zsh/parameter p:history 2>/dev/null && (( ${#commands[perl]} )); then
|
|
|
|
selected="$(printf '%1$s\t%2$s\000' "${(vk)history[@]}" |
|
|
|
|
perl -0 -ne 'if (!$seen{(/^\s*[0-9]+\**\s+(.*)/, $1)}++) { s/\n/\n\t/gm; print; }' |
|
|
|
|
FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort --highlight-line ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} +m --read0") \
|
|
|
|
FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd))"
|
|
|
|
else
|
|
|
|
selected="$(fc -rl 1 | awk '{ cmd=$0; sub(/^[ \t]*[0-9]+\**[ \t]+/, "", cmd); if (!seen[cmd]++) print $0 }' |
|
|
|
|
FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort --highlight-line ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} +m") \
|
|
|
|
FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd))"
|
|
|
|
fi
|
2016-06-15 23:03:48 +00:00
|
|
|
local ret=$?
|
2015-11-04 01:52:44 +00:00
|
|
|
if [ -n "$selected" ]; then
|
2024-05-31 01:43:58 +00:00
|
|
|
if num=$(awk '{print $1; exit}' <<< "$selected" | grep -o '^[1-9][0-9]*'); then
|
|
|
|
zle vi-fetch-history -n $num
|
2024-01-05 01:47:54 +00:00
|
|
|
else # selected is a custom query, not from history
|
|
|
|
LBUFFER="$selected"
|
2015-04-23 13:31:23 +00:00
|
|
|
fi
|
2015-03-13 04:08:42 +00:00
|
|
|
fi
|
2018-10-05 01:56:26 +00:00
|
|
|
zle reset-prompt
|
2016-06-15 23:03:48 +00:00
|
|
|
return $ret
|
2015-03-13 04:08:42 +00:00
|
|
|
}
|
2022-02-23 06:36:49 +00:00
|
|
|
zle -N fzf-history-widget
|
|
|
|
bindkey -M emacs '^R' fzf-history-widget
|
|
|
|
bindkey -M vicmd '^R' fzf-history-widget
|
|
|
|
bindkey -M viins '^R' fzf-history-widget
|
2024-04-17 09:03:12 +00:00
|
|
|
fi
|
2015-03-13 04:08:42 +00:00
|
|
|
|
2020-03-29 16:52:48 +00:00
|
|
|
} always {
|
|
|
|
eval $__fzf_key_bindings_options
|
|
|
|
'unset' '__fzf_key_bindings_options'
|
|
|
|
}
|