Skip to content
Myr

key-bindings.zsh

This file

Path

shell/key-bindings.zsh

203 lines · 7.4 KiB · Shell

Last changed by

fix(zsh): avoid double call for 'chpwd' in alt-cLangLangBartbefore adoptione365764

History of this file

Take it

The walk stopped after 200 commits: lines older than 76efddd point at it.

bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd

lines 1–112why, one step back
1# ____ ____
2# / __/___ / __/3# / /_/_ / / /_4# / __/ / /_/ __/5# /_/ /___/_/ key-bindings.zsh6#7# - $FZF_CTRL_T_COMMAND8# - $FZF_CTRL_T_OPTS9# - $FZF_CTRL_R_COMMAND10# - $FZF_CTRL_R_OPTS11# - $FZF_ALT_C_COMMAND12# - $FZF_ALT_C_OPTS131415# Key bindings16# ------------1718# 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.20if 'zmodload' 'zsh/parameter' 2>'/dev/null' && (( ${+options} )); then21 __fzf_key_bindings_options="options=(${(j: :)${(kv)options[@]}})"22else23 () {24 __fzf_key_bindings_options="setopt"25 'local' '__fzf_opt'26 for __fzf_opt in "${(@)${(@f)$(set -o)}%% *}"; do27 if [[ -o "$__fzf_opt" ]]; then28 __fzf_key_bindings_options+=" -o $__fzf_opt"29 else30 __fzf_key_bindings_options+=" +o $__fzf_opt"31 fi32 done33 }34fi3536'builtin' 'emulate' 'zsh' && 'builtin' 'setopt' 'no_aliases'3738{39if [[ -o interactive ]]; then4041#----BEGIN INCLUDE common.sh42# 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 apply44# the changes. See code comments in "common.sh" for the implementation details.4546__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/null49 builtin printf '%s\n' "${FZF_DEFAULT_OPTS-} $2"50}5152__fzf_exec_awk() {53 if [[ -z ${__fzf_awk-} ]]; then54 __fzf_awk=awk55 if [[ $OSTYPE == solaris* && -x /usr/xpg4/bin/awk ]]; then56 __fzf_awk=/usr/xpg4/bin/awk57 elif command -v mawk > /dev/null 2>&1; then58 local n x y z d59 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=mawk64 fi65 fi66 LC_ALL=C exec "$__fzf_awk" "$@"67}68#----END INCLUDE6970# CTRL-T - Paste the selected file path(s) into the command line71__fzf_select() {72 setopt localoptions pipefail no_aliases 2> /dev/null73 local item74 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; do77 echo -n -E "${(q)item} "78 done79 local ret=$?80 echo81 return $ret82}8384__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}8889fzf-file-widget() {90 LBUFFER="${LBUFFER}$(__fzf_select)"91 local ret=$?92 zle reset-prompt93 return $ret94}95if [[ "${FZF_CTRL_T_COMMAND-x}" != "" ]]; then96 zle -N fzf-file-widget97 bindkey -M emacs '^T' fzf-file-widget98 bindkey -M vicmd '^T' fzf-file-widget99 bindkey -M viins '^T' fzf-file-widget100fi101102# ALT-C - cd into the selected directory103fzf-cd-widget() {104 setopt localoptions pipefail no_aliases 2> /dev/null105 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" ]]; then110 zle redisplay111 return 0112 fi

[shell][zsh] Don't resolve symlinks in ALT-c (#4816)Yi-Yo Chiangbefore adoption7d647c7

lines 113–117why, one step back
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 different115 # 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`.

fix(zsh): avoid double call for 'chpwd' in alt-cLangLangBartbefore adoptione365764

line 118why, one step back
118 dir=$(builtin cd -q >/dev/null -- "${dir}" && echo "${PWD}" || echo "${dir}")

bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd

line 119why, one step back
119 zle push-line # Clear buffer. Auto-restored on next prompt.

[shell][zsh] Don't resolve symlinks in ALT-c (#4816)Yi-Yo Chiangbefore adoption7d647c7

line 120why, one step back
120 BUFFER="builtin cd -- ${(q)dir}"

bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd

lines 121–141why, one step back
121 zle accept-line
122 local ret=$?123 unset dir # ensure this doesn't end up appearing in prompt expansion124 zle reset-prompt125 return $ret126}127if [[ "${FZF_ALT_C_COMMAND-x}" != "" ]]; then128 zle -N fzf-cd-widget129 bindkey -M emacs '\ec' fzf-cd-widget130 bindkey -M vicmd '\ec' fzf-cd-widget131 bindkey -M viins '\ec' fzf-cd-widget132fi133134# CTRL-R - Paste the selected command from history into the command line135fzf-history-widget() {136 local selected extracted_with_perl=0137 setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases no_glob no_sh_glob no_ksharrays extendedglob 2> /dev/null138 # Ensure the module is loaded if not already, and the required features, such139 # as the associative 'history' array, which maps event numbers to full history140 # 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

fix(zsh): fzf-history-widget exit status for perlLangLangBartbefore adoption2327221

line 142why, one step back
142 extracted_with_perl=1

bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd

lines 143–203why, one step back
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 else148 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 fi152 local ret=$?153 local -a cmds154 # Avoid leaking auto assigned values when using backreferences '(#b)'155 local -a mbegin mend match156 if [ -n "$selected" ]; then157 # Heuristic to check if the selected value is from history or a custom query158 if ((( extracted_with_perl )) && [[ $selected == <->$'\t'* ]]) ||159 ((( ! extracted_with_perl )) && [[ $selected == [[:blank:]]#<->( |\* )* ]]); then160 # Split at newlines161 for line in ${(ps:\n:)selected}; do162 if (( extracted_with_perl )); then163 if [[ $line == (#b)(<->)(#B)$'\t'* ]]; then164 (( ${+history[${match[1]}]} )) && cmds+=("${history[${match[1]}]}")165 fi166 elif [[ $line == [[:blank:]]#(#b)(<->)(#B)( |\* )* ]]; then167 # Avoid $history array: lags behind 'fc' on foreign commands (*)168 # https://zsh.org/mla/users/2024/msg00692.html169 # Push BUFFER onto stack; fetch and save history entry from BUFFER; restore170 zle .push-line171 zle vi-fetch-history -n ${match[1]}172 (( ${#BUFFER} )) && cmds+=("${BUFFER}")173 BUFFER=""174 zle .get-line175 fi176 done177 if (( ${#cmds[@]} )); then178 # Join by newline after stripping trailing newlines from each command179 BUFFER="${(pj:\n:)${(@)cmds%%$'\n'#}}"180 CURSOR=${#BUFFER}181 fi182 else # selected is a custom query, not from history183 LBUFFER="$selected"184 fi185 fi186 zle reset-prompt187 return $ret188}189if [[ ${FZF_CTRL_R_COMMAND-x} != "" ]]; then190 if [[ -n ${FZF_CTRL_R_COMMAND-} ]]; then191 echo "warning: FZF_CTRL_R_COMMAND is set to a custom command, but custom commands are not yet supported for CTRL-R" >&2192 fi193 zle -N fzf-history-widget194 bindkey -M emacs '^R' fzf-history-widget195 bindkey -M vicmd '^R' fzf-history-widget196 bindkey -M viins '^R' fzf-history-widget197fi198fi199200} always {201 eval $__fzf_key_bindings_options202 'unset' '__fzf_key_bindings_options'203}