ZSH Gem #16: Command arguments completion

Posted by | Comments (4) | Trackbacks (0)

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. 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:

Trackbacks

No Trackbacks for this entry.

Comments

There have been 4 comments submitted yet. Add one as well!
balta2ar
balta2ar wrote on : (permalink)
What about enabling PID completion for gdb only after -p argument? I did not manage to make it working...
Janek Bevendorff
Janek Bevendorff wrote on : (permalink)
Hi balta2ar, Maybe this could help you: "Writing Zsh completion functions":http://www.linux-mag.com/id/1106/
Steve
Steve wrote on : (permalink)
Hi Janek, could you please tag this article so that readers are able to reach the full list of this series more easily? Thank you!
Janek Bevendorff
Janek Bevendorff wrote on : (permalink)
Hi Steve, all articles of this series are within the category "Advent calendar 2011". That should be enough have them all on one place. :-)

Write a comment:

E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

By submitting a comment, you agree to our privacy policy.

Design and Code Copyright © 2010-2024 Janek Bevendorff Content on this site is published under the terms of the GNU Free Documentation License (GFDL). You may redistribute content only in compliance with these terms.