Proxy command completion in zsh
Commands 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 wordsremoves the first element of thewordsarray (sudo git statusbecomesgit status)(( CURRENT-- ))decreases the index of the word focused by tab completion. Updatingwordsdoesn't automatically updatesCURRENT_normalre-runs the completion functions with the currentwordsandCURRENTcontext.