2 ms·
I have had this in my .zshrc for years: # Quick ../../.. from https://github.com/blueyed/oh-my-zsh resolve-alias() { # Recursively resolve aliases and
by Symbiote 9y ago
I have had this in my .zshrc for years:
# Quick ../../.. from https://github.com/blueyed/oh-my-zsh
resolve-alias() {
# Recursively resolve aliases and echo the command.
typeset -a cmd
cmd=(${(z)1})
while (( ${+aliases[$cmd[1]]} )) \
&& [[ ${aliases[$cmd[1]]} != $cmd ]]; do
cmd=(${(z)aliases[${cmd[1]}]})
done
echo $cmd
}
rationalise-dot() {
# Auto-expand "..." to "../..", "...." to "../../.." etc.
# It skips certain commands (git, tig, p4).
#
# resolve-alias is defined in a separate function.
local MATCH # keep the regex match from leaking to the environment.
# Skip pasted text.
if (( PENDING > 0 )); then
zle self-insert
return
fi
if [[ $LBUFFER =~ '(^|/| ||'$'\n''|\||;|&)\.\.$' ]] \
&& ! [[ $(resolve-alias $LBUFFER) =~ '(git|tig|p4)' ]]; then
LBUFFER+=/
zle self-insert
zle self-insert
else
zle self-insert
fi
}
zle -N rationalise-dot
bindkey . rationalise-dot
bindkey -M isearch . self-insert 2>/dev/null
For doing "cd ../.." I type "cd ...", each extra "." adds a "/.." to the argument.
It works for any command, cp/mv/whatever.
There may be nicer versions, certainly shorter versions are there if I search "rationalise-dot" in Google.