commit 57c48eb7b2ae0053260963fb72b42d31bc9fcaf6
parent ff540732764e5e1766a896b6069537a2159a3d0d
Author: Oscar Benedito <oscar@oscarbenedito.com>
Date:   Thu,  9 Dec 2021 17:42:36 +0100

Use installed FZF keybindings file for ZSH

Diffstat:
M.config/zsh/.zshrc | 5++++-
D.config/zsh/key-bindings.zsh | 121-------------------------------------------------------------------------------
2 files changed, 4 insertions(+), 122 deletions(-)

diff --git a/.config/zsh/.zshrc b/.config/zsh/.zshrc @@ -37,8 +37,11 @@ bindkey -M menuselect 'j' vi-down-line-or-history [ -f "$XDG_CONFIG_HOME/zsh/aliases" ] && source "$XDG_CONFIG_HOME/zsh/aliases" # Process fzf key bindings -[ -f "$XDG_CONFIG_HOME/zsh/key-bindings.zsh" ] && source "$XDG_CONFIG_HOME/zsh/key-bindings.zsh" +# In other distributions, this file might be found at /usr/share/fzf/shell/key-bindings.zsh +[ -f "/usr/share/fzf/key-bindings.zsh" ] && \ + source "/usr/share/fzf/key-bindings.zsh" 2> /dev/null # Include the highlighting plug-in +# In other distributions, this file might be found at /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh [ -f "/usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" ] && \ source "/usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" 2> /dev/null diff --git a/.config/zsh/key-bindings.zsh b/.config/zsh/key-bindings.zsh @@ -1,121 +0,0 @@ -# ____ ____ -# / __/___ / __/ -# / /_/_ / / /_ -# / __/ / /_/ __/ -# /_/ /___/_/ 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 - -# Key bindings -# ------------ - -# 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 - -# CTRL-T - Paste the selected file path(s) into the command line -__fsel() { - 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 \ - -o -type f -print \ - -o -type d -print \ - -o -type l -print 2> /dev/null | cut -b3-"}" - setopt localoptions pipefail no_aliases 2> /dev/null - eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS" $(__fzfcmd) -m "$@" | while read item; do - echo -n "${(q)item} " - done - local ret=$? - echo - return $ret -} - -__fzfcmd() { - [ -n "$TMUX_PANE" ] && { [ "${FZF_TMUX:-0}" != 0 ] || [ -n "$FZF_TMUX_OPTS" ]; } && - echo "fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- " || echo "fzf" -} - -fzf-file-widget() { - LBUFFER="${LBUFFER}$(__fsel)" - local ret=$? - zle reset-prompt - return $ret -} -zle -N fzf-file-widget -bindkey '^T' fzf-file-widget - -# 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 - -# ALT-C - cd into the selected directory -fzf-cd-widget() { - 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 \ - -o -type d -print 2> /dev/null | cut -b3-"}" - setopt localoptions pipefail no_aliases 2> /dev/null - 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 - cd "$dir" - unset dir # ensure this doesn't end up appearing in prompt expansion - local ret=$? - zle fzf-redraw-prompt - return $ret -} -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() { - local selected num - setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases 2> /dev/null - selected=( $(fc -rl 1 | perl -ne 'print if !$seen{(/^\s*[0-9]+\s+(.*)/, $1)}++' | - 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)) ) - local ret=$? - if [ -n "$selected" ]; then - num=$selected[1] - if [ -n "$num" ]; then - zle vi-fetch-history -n $num - fi - fi - zle reset-prompt - return $ret -} -zle -N fzf-history-widget -bindkey '^R' fzf-history-widget - -} always { - eval $__fzf_key_bindings_options - 'unset' '__fzf_key_bindings_options' -}