From 14f615f5e197c2d922cf5ec6c8349e1614c6f221 Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Thu, 22 Sep 2022 15:15:07 +0200 Subject: [PATCH] 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. --- .config/zsh/zshrc.d/60-keys.zsh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.config/zsh/zshrc.d/60-keys.zsh b/.config/zsh/zshrc.d/60-keys.zsh index 55b44a3..fd82190 100644 --- a/.config/zsh/zshrc.d/60-keys.zsh +++ b/.config/zsh/zshrc.d/60-keys.zsh @@ -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