Custom Tab Completion
I'm sure you already know about tab completion: type part of a
command name and hit TAB and you get its matches. Leave a space and
then hit TAB, and you get filenames. Wonderful stuff. But what if
YOUR command wants user names instead of file names?
The newer versions of Bash (above 2.04) have Custom Tab
Completion, which means that you can control what happens when TAB
is typed after a command. You'll find the applicable sections in
man or info for "bash" under the the descriptions of "compgen" and
The "compgen" command is the magic behind all this stuff. Its
usage is simple: compgen options word. For example, try this:
compgen -A user -- r
The "--" separates options from the word we are trying to match
("r"). I chose "r" because you probably have at least a "root"
user. Try it with other letters or words that will match your user
compgen -A user -- to
Or leave it blank:
compgen -A user
root bin daemon adm lp sync shutdown halt mail news uucp operator
games gopher ftp nobody rpm vcsa nscd sshd rpc rpcuser nfsnobody
mailnull smmsp pcap xfs ntp gdm desktop apache webalizer squid named
Compgen can use the results of other commands or variables:
compgen -W '$( mount | cut -d" " -f 3)'
The options compgen recognizes come from the "complete"
built-in. There are quite a few - see the bash man pages for all of
them because I'll only discuss a few here. Let's try some of
$ compgen -W 'appl arctic bob sam' -- a
$ compgen -A signal -- SIGA
$ compgen -A service -- power
Fun, right? Obviously "compgen" could be very useful inside
scripts to offer dynamic choices with little effort on your part.
But it's even more fun when you use it with tab completion.
Custom Tab Completion
The first thing you have to do is enable custom tab completion.
shopt -s progcomp
The best place to do that is .bashrc and that's also where
you'll want to put the rest of what we do. You CAN do it elsewhere,
of course - there's nothing magic about having it in .bashrc,
that's just a convenient place.
OK, now we have a script we've written. Let's called that
# doesn't do much
Be sure to put "myprog" somewhere in your PATH - shell tab
completion will not work otherwise.
Next, we define a function that we will use for tab completion
COMPREPLY=($(compgen -A user -- $curw))
In this function, "$curw" will be what you typed before hitting
will end up (once we've activated this) with $curw being "to".
We get that from the built-in $COMP_WORDS array. We then set
COMPREPLY to be the output of compgen. Finally, we need to activate
this and associate it with our command (add this to .bashrc
complete -F _myprog -o dirnames myprog
That's it. Check your work, and type "bash" to start up a new
shell (so your .bashrc gets read) and type "myprog" followed by a
space and a TAB. You should see user names listed instead of file
names. If you did something wrong, you'll either see a complaint
from compgen or a list of dirnames (because our "complete" included
-o dirnames). Debug by running your compgen on the command line as
shown in the first part of this article.
Nothing prevents you from adding custom tab completion to normal
system commands. Because the choices it offers can be generated by
scripts you write, the possibilities are endless. How about a
custom tab function for ssh that lists the places you normally ssh
to? Or the same idea for ftp?
has some pre-made custom tab completion scripts that you can
download to use or get ideas from.
If this page was useful to you, please help others find it:
More Articles by Tony Lawrence
- Find me on Google+
Have you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site:
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
Publishing your articles here
Jump to Comments
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
I am a Kerio reseller. Articles here related to Kerio products reflect my honest opinion, but I do have an obvious interest in selling those products also.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.