From 38a2076b893213f6d29b91299c5edbe5237a4320 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 16 Jun 2016 01:03:48 +0200 Subject: [PATCH] zsh: pass through exit code from widgets This allows to have a custom widget like the following, which would additionally accept the line, but only in case of entries being selected: fzf-file-widget-with-accept() { zle fzf-file-widget if [[ "$?" == 0 ]] && (( $#BUFFER )); then zle accept-line fi } zle -N fzf-file-widget-with-accept bindkey '\e^T' fzf-file-widget-with-accept With this `t` will launch fzf, and simulate the pressing of "Enter" afterwards. --- shell/key-bindings.zsh | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/shell/key-bindings.zsh b/shell/key-bindings.zsh index 35e5f19..ea65c0c 100644 --- a/shell/key-bindings.zsh +++ b/shell/key-bindings.zsh @@ -8,10 +8,13 @@ __fsel() { -o -type f -print \ -o -type d -print \ -o -type l -print 2> /dev/null | sed 1d | cut -b3-"}" + setopt localoptions pipefail eval "$cmd | $(__fzfcmd) -m $FZF_CTRL_T_OPTS" | while read item; do echo -n "${(q)item} " done + local ret=$? echo + return $ret } __fzfcmd() { @@ -20,7 +23,9 @@ __fzfcmd() { fzf-file-widget() { LBUFFER="${LBUFFER}$(__fsel)" + local ret=$? zle redisplay + return $ret } zle -N fzf-file-widget bindkey '^T' fzf-file-widget @@ -29,8 +34,11 @@ bindkey '^T' fzf-file-widget fzf-cd-widget() { local cmd="${FZF_ALT_C_COMMAND:-"command find -L . \\( -path '*/\\.*' -o -fstype 'dev' -o -fstype 'proc' \\) -prune \ -o -type d -print 2> /dev/null | sed 1d | cut -b3-"}" + setopt localoptions pipefail cd "${$(eval "$cmd | $(__fzfcmd) +m $FZF_ALT_C_OPTS"):-.}" + local ret=$? zle reset-prompt + return $ret } zle -N fzf-cd-widget bindkey '\ec' fzf-cd-widget @@ -38,8 +46,9 @@ 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 + setopt localoptions noglobsubst pipefail selected=( $(fc -l 1 | eval "$(__fzfcmd) +s --tac +m -n2..,.. --tiebreak=index --toggle-sort=ctrl-r $FZF_CTRL_R_OPTS -q ${(q)LBUFFER}") ) + local ret=$? if [ -n "$selected" ]; then num=$selected[1] if [ -n "$num" ]; then @@ -47,6 +56,7 @@ fzf-history-widget() { fi fi zle redisplay + return $ret } zle -N fzf-history-widget bindkey '^R' fzf-history-widget