zsh:keys:ratio_dots: Fix preview of first word

Previously the path preview misbehaved when typing dots on the first
shell word (e.g. executing a script in a parent directory). This
happened as the `(z)` parameter expansion flag does not expand to an
array rather a scalar. Because of that, `[-1]` returns the last
character.

Work around this issue by prepending LBUFFER with a dummy word, so that
it always contains at least 2 words.
This commit is contained in:
2022-09-22 15:15:07 +02:00
parent 9a5536c5c5
commit 14f615f5e1

View File

@@ -100,9 +100,13 @@ function rationalize_dots {
return
fi
# NOTE: This is a hack to fix the (z) expansion flag if the path is the only
# word in LBUFFER, as then [:-1] treats the expansion as a scalar and
# returns the last character (i.e. always `/`).
local lbuffer_words="x $LBUFFER"
# Print currently typed path as absolute path with "collapsed"/reversed
# filename expansion.
zle -M "${(D)${(z)LBUFFER}[-1]:a}"
zle -M "${(D)${(z)lbuffer_words}[-1]:a}"
}
zle -N rationalize_dots
bindkey . rationalize_dots