ZSH has lots of great expansion and auto completion features and I have shown you many of them. One more completion feature I want to show you is command arguments completion. With this completion of command line parameters you can auto complete the arguments of your command line tools. Many completion definitions are already included in ZSH, many more can be installed via your package manager.
The great thing about ZSH command line parameter completion is that it also shows you what the parameters do.
But first make sure the completion engine is loaded:
autoload -U compinit && compinit
Now enter some command like
cp, write a dash (which introduces an option argument) and hit TAB:
% cp -<TAB>
You might be asked whether you really want to show all parameters (yes, you do) and then all available options are listed including descriptions of what they do:
--archive -a -- same as -dpR --backup -b -- backup --copy-contents -- copy contents of special files when recursive --dereference -L -- always follow symbolic links --force -f -- remove and retry for destinations that cannot be opened --interactive -i -- prompt before overwrite --link -l -- link files instead of copying --no-clobber -n -- do not overwrite an existing file --no-dereference -P -- never follow symbolic links --no-preserve -- don't preserve specified attributes --no-target-directory -T -- treat DEST as a normal file --one-file-system -x -- stay on this file system --parents -- append source path to target directory --preserve -- preserve specified attributes --recursive -r -R -- copy directories recursively --reflink -- control clone/CoW copies --remove-destination -- remove each existing destination file before attempting to open it --sparse -- control creation of sparse files --strip-trailing-slashes -- remove any trailing slashes from each source argument --suffix -S -- override the usual backup suffix --symbolic-link -s -- make symbolic links instead of copies of non-directories --target-directory -t -- copy all source arguments into target directory --update -u -- copy only when source is newer than destination or destination is missing --verbose -v -- explain what is being done -H -- follow command-line symbolic links -d -- same as --no-dereference --preserve=links -p -- same as --preserve=mode,ownership,timestamps --help --version
Isn't that great? This is especially useful when you need help for some shell built-ins which don't have a separate man page (searching the ZSH man pages can take hours, days or weeks or at least a very, very long time).
But that's no everything you can do. Also parameters not starting with dashes can be completed (as long as the definition files for them exist). This also work for
cp (here you can complete source and destination files), but an even better example is
kill expects the PIDs of the processes to send signals to and ZSH is able to complete them:
% kill 2<TAB>
That shows a list of all PIDs starting with 2 (or completes directly if there is only one) as well as some other information about the processes. But we can do even more. We can also enter the name of the command (or parts of it):
% kill firef<TAB>
This will most likely complete to firefox and show all PIDs for the program firefox (or any other command starting with firef). If you think, you've already read that somewhere, you might be right. I told you about that a few days ago when I talked about menu selection.
In my opinion, command argument completion is one of the most awesome features in ZSH and because it's so wonderful, I want to present you one even more exciting gem:
Argument completion for
kill is already great, but what if we could make it even better? We can! The output of all completion features is customizable with the
zstyle built-in and that's what we're going to do. Do you know the tree view of the
ps command? Wouldn't it be smart to have this in our
kill completion as well? Here you go:
zstyle ':completion:*:*:kill:*:processes' command 'ps --forest -e -o pid,user,tty,cmd'
When you now invoke the kill completion you'll see the PIDs of the processes, their owners, the controlling ttys, the commands and all that in a nice tree view so you can easily see which child process belongs to which parent process. If you like, you can customize this even more. Just modify the arguments for
ps in the
zstyle code above.
Read more about command arguments completion:
- zsh.sf.net: Completion System
- zsh.sf.net: Completion System Configuration
- zsh.sf.net: Configuring completion using styles
- zsh.sf.net: Completion, old and new