I have a custom script that takes hostnames as parameters. I know that I can easily copy the existing completion of ssh like this:
compdef myscript=ssh
But that only enables completion of the 1st parameter. Is there an easy way to enable the same completion for all parameters?
I’m not aware of an easy method to enable completion for a custom command. Assuming you’ve got a command
foowith a bunch of allowable argumentsbar,basorbaz, then the completion is easy: you can either havefoo bar,foo bas, orfoo baz. If they’re notor‘d, though, you could have any combination of the three.It gets somewhat worse when you’ve got a ‘depth’ of more than 1 (
barcan take argumentscar,casandcaz, for example).In
zsh, my general understanding is that completion for commands is detailed incompletion functions. These functions are application specific, because the arguments for each application are specific to those applications. As an example, thetmux(a terminal multiplexer, similar toscreen, in case you’re not familiar) has a completion function that’s fairly complex: here’s a link.If you want to write your own completion functions, the documentation is available and accessible. Here are a bunch of links that I’m (slowly) working my way through – they’ll definitely tell you how to get completion working, but there’s a lot of reading. The Z-Shell is a complex beast.
You’re specifically interested in enabling completion for hostname-like arguments, and you’ve singled out
sshas the idea. Thezshcompletion function forsshis defined inCompletion/Unix/Command/_ssh, if you’ve got the ZSH source. If not, here’s a link.Of course, this SO question is rather similar.
compdefalone may do what you want, ifmyscriptandsshparameters are identical enough.