Skip to content

Instantly share code, notes, and snippets.

@mattmc3
Created September 15, 2022 12:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattmc3/336fd7720ed040e65439a671706c0525 to your computer and use it in GitHub Desktop.
Save mattmc3/336fd7720ed040e65439a671706c0525 to your computer and use it in GitHub Desktop.
Zsh auto_cd-like feature implementation
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