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 "complete".


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

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

$ 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. That's simply

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 "myprog":

# myprog
# doesn't do much
echo Hi!

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 with "myprog":

local curw
COMPREPLY=($(compgen -A user -- $curw))
return 0

In this function, "$curw" will be what you typed before hitting TAB:

myprog to

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 also):

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.

Other commands

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.

Got something to add? Send me email.

(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Custom Tab Completion


More Articles by

Find me on Google+

© Tony Lawrence

Fri Dec 11 23:19:12 2009: 7747   zolo


I would like to ask is it possible, or how to possible to use the completion future in case of the command "read". For example I would like to read a variable from shell script (bash) and I would like to use the <TAB> for complete -for example- specific file names from a specified directory. I used google for find answer to this idea, but after 2-3 hours I didn't find any useful according this specific "problem". I would be really thankful if you could help or give me some hint..

Fri Dec 11 23:42:51 2009: 7748   TonyLawrence

I don't think so.

You'll be reading user input and you want them to type part of a file name and hit tab to expand the rest. You'd probably need more than bash for that.

Oops: I just looked more closely: "read -e" is supposed to use the readline library, so would do tab etc.

Sat Dec 12 16:32:26 2009: 7749   zolo

Thanks for your fast answer. Finally I wrote a function what read 1 character from input, then a self-written auto compleate way its complease the input string according to found file names. Can not use tab, enter, but working well. Special characters can use for backspace or start the input reading from beginning. Not 100% the same, but in case of character input (no tab, enter, arrows or backspace) its work exactly the same way.

Sat Dec 12 17:37:04 2009: 7750   TonyLawrence

I'm sure it would be a lot easier in a more powerful language, but I'm impressed you got close. Fell free to share it here if you like - wrap it in pre, /pre; tags for best results.

Sat Dec 12 17:47:33 2009: 7751   TonyLawrence

I just looked more closely: "read -e" is supposed to use the readline library, so would do tab etc.

Sat Dec 12 19:28:26 2009: 7755   TonyLawrence

Yes, that works:

read -e -p "Enter filename  [$filename] " tmpfile 
[ "x$tmpfile" = "x" ] || filename=$tmpfile 

Sat Dec 12 21:14:16 2009: 7756   zolo

my script is below. Too fast typing can cause probles. the ":>" prompt always show what have inside already. The code is not "perfect", but for me seems great,
Tested with 85 prp files (hostnames.prp)

anyway thanks for your help! btw is it possible to check difference between special characters what I write to the "read" command? I mean I typed tab or I typed enter, and so on..
I tried to check with octal codes and with simple echo (for enter) but it didnt work


----------- ---------------

# this script checking the actual directory, for .prp extension files only (self ext. for future use)
echo -n ":> "
while [ "$valtozo" != "done" ];do

read -n1 -s valtozo
#if I want to restart typing from the first character, just write a %. Can replace if filenames will contains %. (in my case, wont).
if [ "$valtozo" == "%" ];then valtoz="";valtozo="a";echo;echo -n ":> ";continue;fi
valtoz=`echo "$valtoz$valtozo"`

#for - checking more and more character on found filenames, when not all same on that character, then show the list and read a letter/character from user
for i in `seq 50`;do
#if found more then 1 file, and the "i"th character is same on all found name, then continue (start again with increased length check)
if [ `ls -1 "$valtoz"* 2>/dev/null|grep prp 2>/dev/null|wc -l` -gt 1 -a `ls -1 "$valtoz"* 2>/dev/null|grep prp|uniq -c -w$i 2>/dev/null|wc -l 2>/dev/null` -eq 1 ];then
#checking wheather only 1 matching file name I found
if [ `ls -1 "$valtoz"* 2>/dev/null|grep prp|wc -l 2>/dev/null` -eq 1 ];then
valtoz=`ls -1 "$valtoz"* 2>/dev/null|grep prp|sed 's/\.prp//g'`
echo ":> $valtoz"
#done string needed to escape from the while loop
# put to "valtoz" variable a string what is same in filenames. After will read one more character from user
valtoz=`ls -1 "$valtoz"* 2>/dev/null|grep prp|head -1 2>/dev/null|cut -c1-$((i-1)) 2>/dev/null`
#listing the matching file names, for easy select which ones remains with the same beginning. column makes the output fancy
ls -1 "$valtoz"* 2>/dev/null|grep prp|sed 's/.prp//g' 2>/dev/null|column
#write out the file name part what I already typed + found by itself as same characters in filenames
echo -n ":> $valtoz"

if [ -e "$valtoz.prp" ];then
echo ittvagyok

#echo -n "$valtozo"

echo "Enter the filename"
echo -n ":> "
echo "Loading file $valtoz.prp"


Sat Dec 12 21:42:35 2009: 7757   TonyLawrence

I think you missed my comment above - use "read -e" to get tab to work. See sample script in my comments above.

Sat Dec 12 21:57:23 2009: 7758   zolo


I didnt tried it and when i read that i was almost done with my script. I thought its working only with the suggested filename. Just tried when you told me again, and yes, its do the same, perfectly and in one line. ("read -e" line was enough, and tab worked.)

thanks again :)

Kerio Samepage

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

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.

Contact us

privacy policy