Proxy command completion in zsh
03 Apr 2023Commands like sudo
take other commands as input and run them in a specific context. I also have a command similar, called lazyloadNvm
that runs nvm use
when needed.
I wanted zsh to suggest completion for the passed commands instead of completion for the initial command (sudo
or lazyloadNvm
).
The solution was to create a _nvm-lazyload
comdef file like this:
#compdef
function _nvm-lazyload() {
# Remove the first word (lazyloadNvm)
shift words
# Update which word is currently being focused for tab completion
(( CURRENT-- ))
# Re-run completion with the new input
_normal
}
shift words
removes the first element of thewords
array (sudo git status
becomesgit status
)(( CURRENT-- ))
decreases the index of the word focused by tab completion. Updatingwords
doesn't automatically updatesCURRENT
_normal
re-runs the completion functions with the currentwords
andCURRENT
context.
Want to add something ? Feel free to get in touch on Twitter : @pixelastic