| 1 |
# ____ ____ |
| 2 |
# / __/___ / __/ |
| 3 |
# / /_/_ / / /_ |
| 4 |
# / __/ / /_/ __/ |
| 5 |
# /_/ /___/_/ key-bindings.zsh |
| 6 |
# |
| 7 |
# - $FZF_CTRL_T_COMMAND |
| 8 |
# - $FZF_CTRL_T_OPTS |
| 9 |
# - $FZF_CTRL_R_COMMAND |
| 10 |
# - $FZF_CTRL_R_OPTS |
| 11 |
# - $FZF_ALT_C_COMMAND |
| 12 |
# - $FZF_ALT_C_OPTS |
| 13 |
|
| 14 |
|
| 15 |
# Key bindings |
| 16 |
# ------------ |
| 17 |
|
| 18 |
# The code at the top and the bottom of this file is the same as in completion.zsh. |
| 19 |
# Refer to that file for explanation. |
| 20 |
if 'zmodload' 'zsh/parameter' 2>'/dev/null' && (( ${+options} )); then |
| 21 |
__fzf_key_bindings_options="options=(${(j: :)${(kv)options[@]}})" |
| 22 |
else |
| 23 |
() { |
| 24 |
__fzf_key_bindings_options="setopt" |
| 25 |
'local' '__fzf_opt' |
| 26 |
for __fzf_opt in "${(@)${(@f)$(set -o)}%% *}"; do |
| 27 |
if [[ -o "$__fzf_opt" ]]; then |
| 28 |
__fzf_key_bindings_options+=" -o $__fzf_opt" |
| 29 |
else |
| 30 |
__fzf_key_bindings_options+=" +o $__fzf_opt" |
| 31 |
fi |
| 32 |
done |
| 33 |
} |
| 34 |
fi |
| 35 |
|
| 36 |
'builtin' 'emulate' 'zsh' && 'builtin' 'setopt' 'no_aliases' |
| 37 |
|
| 38 |
{ |
| 39 |
if [[ -o interactive ]]; then |
| 40 |
|
| 41 |
#----BEGIN INCLUDE common.sh |
| 42 |
# NOTE: Do not directly edit this section, which is copied from "common.sh". |
| 43 |
# To modify it, one can edit "common.sh" and run "./update.sh" to apply |
| 44 |
# the changes. See code comments in "common.sh" for the implementation details. |
| 45 |
|
| 46 |
__fzf_defaults() { |
| 47 |
builtin printf '%s\n' "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1" |
| 48 |
command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null |
| 49 |
builtin printf '%s\n' "${FZF_DEFAULT_OPTS-} $2" |
| 50 |
} |
| 51 |
|
| 52 |
__fzf_exec_awk() { |
| 53 |
if [[ -z ${__fzf_awk-} ]]; then |
| 54 |
__fzf_awk=awk |
| 55 |
if [[ $OSTYPE == solaris* && -x /usr/xpg4/bin/awk ]]; then |
| 56 |
__fzf_awk=/usr/xpg4/bin/awk |
| 57 |
elif command -v mawk > /dev/null 2>&1; then |
| 58 |
local n x y z d |
| 59 |
IFS=' .' read -r n x y z d <<< $(command mawk -W version 2> /dev/null) |
| 60 |
[[ $n == mawk ]] && |
| 61 |
(((x * 1000 + y) * 1000 + z >= 1003004)) 2> /dev/null && |
| 62 |
((d >= 20230302)) 2> /dev/null && |
| 63 |
__fzf_awk=mawk |
| 64 |
fi |
| 65 |
fi |
| 66 |
LC_ALL=C exec "$__fzf_awk" "$@" |
| 67 |
} |
| 68 |
#----END INCLUDE |
| 69 |
|
| 70 |
# CTRL-T - Paste the selected file path(s) into the command line |
| 71 |
__fzf_select() { |
| 72 |
setopt localoptions pipefail no_aliases 2> /dev/null |
| 73 |
local item |
| 74 |
FZF_DEFAULT_COMMAND=${FZF_CTRL_T_COMMAND:-} \ |
| 75 |
FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --walker=file,dir,follow,hidden --scheme=path" "${FZF_CTRL_T_OPTS-} -m") \ |
| 76 |
FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) "$@" < /dev/tty | while read -r item; do |
| 77 |
echo -n -E "${(q)item} " |
| 78 |
done |
| 79 |
local ret=$? |
| 80 |
echo |
| 81 |
return $ret |
| 82 |
} |
| 83 |
|
| 84 |
__fzfcmd() { |
| 85 |
[ -n "${TMUX_PANE-}" ] && { [ "${FZF_TMUX:-0}" != 0 ] || [ -n "${FZF_TMUX_OPTS-}" ]; } && |
| 86 |
echo "fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- " || echo "fzf" |
| 87 |
} |
| 88 |
|
| 89 |
fzf-file-widget() { |
| 90 |
LBUFFER="${LBUFFER}$(__fzf_select)" |
| 91 |
local ret=$? |
| 92 |
zle reset-prompt |
| 93 |
return $ret |
| 94 |
} |
| 95 |
if [[ "${FZF_CTRL_T_COMMAND-x}" != "" ]]; then |
| 96 |
zle -N fzf-file-widget |
| 97 |
bindkey -M emacs '^T' fzf-file-widget |
| 98 |
bindkey -M vicmd '^T' fzf-file-widget |
| 99 |
bindkey -M viins '^T' fzf-file-widget |
| 100 |
fi |
| 101 |
|
| 102 |
# ALT-C - cd into the selected directory |
| 103 |
fzf-cd-widget() { |
| 104 |
setopt localoptions pipefail no_aliases 2> /dev/null |
| 105 |
local dir="$( |
| 106 |
FZF_DEFAULT_COMMAND=${FZF_ALT_C_COMMAND:-} \ |
| 107 |
FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --walker=dir,follow,hidden --scheme=path" "${FZF_ALT_C_OPTS-} +m") \ |
| 108 |
FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) < /dev/tty)" |
| 109 |
if [[ -z "$dir" ]]; then |
| 110 |
zle redisplay |
| 111 |
return 0 |
| 112 |
fi |
| 113 |
# Use subshell expansion to get the absolute PWD of the target dir. |
| 114 |
# This allows the recorded shell history to be reused even from a different |
| 115 |
# working directory. |
| 116 |
# If failed, fallback to the unexpanded path to surface the error to the user. |
| 117 |
# NOTE: Don't use the `:a` modifier as it resolves symlinks like `pwd -P`. |
| 118 |
dir=$(builtin cd -q >/dev/null -- "${dir}" && echo "${PWD}" || echo "${dir}") |
| 119 |
zle push-line # Clear buffer. Auto-restored on next prompt. |
| 120 |
BUFFER="builtin cd -- ${(q)dir}" |
| 121 |
zle accept-line |
| 122 |
local ret=$? |
| 123 |
unset dir # ensure this doesn't end up appearing in prompt expansion |
| 124 |
zle reset-prompt |
| 125 |
return $ret |
| 126 |
} |
| 127 |
if [[ "${FZF_ALT_C_COMMAND-x}" != "" ]]; then |
| 128 |
zle -N fzf-cd-widget |
| 129 |
bindkey -M emacs '\ec' fzf-cd-widget |
| 130 |
bindkey -M vicmd '\ec' fzf-cd-widget |
| 131 |
bindkey -M viins '\ec' fzf-cd-widget |
| 132 |
fi |
| 133 |
|
| 134 |
# CTRL-R - Paste the selected command from history into the command line |
| 135 |
fzf-history-widget() { |
| 136 |
local selected extracted_with_perl=0 |
| 137 |
setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases no_glob no_sh_glob no_ksharrays extendedglob 2> /dev/null |
| 138 |
# Ensure the module is loaded if not already, and the required features, such |
| 139 |
# as the associative 'history' array, which maps event numbers to full history |
| 140 |
# lines, are set. Also, make sure Perl is installed for multi-line output. |
| 141 |
if zmodload -F zsh/parameter p:{commands,history} 2>/dev/null && (( ${+commands[perl]} )); then |
| 142 |
extracted_with_perl=1 |
| 143 |
selected="$(printf '%s\t%s\000' "${(kv)history[@]}" | |
| 144 |
perl -0 -ne 'if (!$seen{(/^\s*[0-9]+\**\t(.*)/s, $1)}++) { s/\n/\n\t/g; print; }' | |
| 145 |
FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort,alt-r:toggle-raw --wrap-sign '\t↳ ' --highlight-line --multi ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} --read0") \ |
| 146 |
FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd))" |
| 147 |
else |
| 148 |
selected="$(fc -rl 1 | __fzf_exec_awk '{ cmd=$0; sub(/^[ \t]*[0-9]+\**[ \t]+/, "", cmd); if (!seen[cmd]++) print $0 }' | |
| 149 |
FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort,alt-r:toggle-raw --wrap-sign '\t↳ ' --highlight-line --multi ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER}") \ |
| 150 |
FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd))" |
| 151 |
fi |
| 152 |
local ret=$? |
| 153 |
local -a cmds |
| 154 |
# Avoid leaking auto assigned values when using backreferences '(#b)' |
| 155 |
local -a mbegin mend match |
| 156 |
if [ -n "$selected" ]; then |
| 157 |
# Heuristic to check if the selected value is from history or a custom query |
| 158 |
if ((( extracted_with_perl )) && [[ $selected == <->$'\t'* ]]) || |
| 159 |
((( ! extracted_with_perl )) && [[ $selected == [[:blank:]]#<->( |\* )* ]]); then |
| 160 |
# Split at newlines |
| 161 |
for line in ${(ps:\n:)selected}; do |
| 162 |
if (( extracted_with_perl )); then |
| 163 |
if [[ $line == (#b)(<->)(#B)$'\t'* ]]; then |
| 164 |
(( ${+history[${match[1]}]} )) && cmds+=("${history[${match[1]}]}") |
| 165 |
fi |
| 166 |
elif [[ $line == [[:blank:]]#(#b)(<->)(#B)( |\* )* ]]; then |
| 167 |
# Avoid $history array: lags behind 'fc' on foreign commands (*) |
| 168 |
# https://zsh.org/mla/users/2024/msg00692.html |
| 169 |
# Push BUFFER onto stack; fetch and save history entry from BUFFER; restore |
| 170 |
zle .push-line |
| 171 |
zle vi-fetch-history -n ${match[1]} |
| 172 |
(( ${#BUFFER} )) && cmds+=("${BUFFER}") |
| 173 |
BUFFER="" |
| 174 |
zle .get-line |
| 175 |
fi |
| 176 |
done |
| 177 |
if (( ${#cmds[@]} )); then |
| 178 |
# Join by newline after stripping trailing newlines from each command |
| 179 |
BUFFER="${(pj:\n:)${(@)cmds%%$'\n'#}}" |
| 180 |
CURSOR=${#BUFFER} |
| 181 |
fi |
| 182 |
else # selected is a custom query, not from history |
| 183 |
LBUFFER="$selected" |
| 184 |
fi |
| 185 |
fi |
| 186 |
zle reset-prompt |
| 187 |
return $ret |
| 188 |
} |
| 189 |
if [[ ${FZF_CTRL_R_COMMAND-x} != "" ]]; then |
| 190 |
if [[ -n ${FZF_CTRL_R_COMMAND-} ]]; then |
| 191 |
echo "warning: FZF_CTRL_R_COMMAND is set to a custom command, but custom commands are not yet supported for CTRL-R" >&2 |
| 192 |
fi |
| 193 |
zle -N fzf-history-widget |
| 194 |
bindkey -M emacs '^R' fzf-history-widget |
| 195 |
bindkey -M vicmd '^R' fzf-history-widget |
| 196 |
bindkey -M viins '^R' fzf-history-widget |
| 197 |
fi |
| 198 |
fi |
| 199 |
|
| 200 |
} always { |
| 201 |
eval $__fzf_key_bindings_options |
| 202 |
'unset' '__fzf_key_bindings_options' |
| 203 |
} |