Created
September 15, 2022 12:22
-
-
Save mattmc3/336fd7720ed040e65439a671706c0525 to your computer and use it in GitHub Desktop.
Zsh auto_cd-like feature implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setopt no_auto_cd | |
function auto_dircmd { | |
# Run a command when PS1 contains a valid directory, similar to auto_cd | |
if [[ -d "$PWD/$BUFFER" ]] && | |
(( ! $+functions[$BUFFER] )) && | |
(( ! $+commands[$BUFFER] )) | |
then | |
BUFFER="ls \"./$BUFFER\"" | |
fi | |
} | |
# If the accept-line wrapper already exists don't redefine it | |
(( ! ${+functions[_auto_dircmd_accept-line]} )) || return 0 | |
case "$widgets[accept-line]" in | |
# Override the current accept-line widget, calling the old one | |
user:*) zle -N _auto_dircmd_orig_accept-line "${widgets[accept-line]#user:}" | |
function _auto_dircmd_accept-line() { | |
auto_dircmd | |
zle _auto_dircmd_orig_accept-line -- "$@" | |
} ;; | |
# If no user widget defined, call the original accept-line widget | |
builtin) function _auto_dircmd_accept-line() { | |
auto_dircmd | |
zle .accept-line | |
} ;; | |
esac | |
zle -N accept-line _auto_dircmd_accept-line |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment