Welcome to WebProNews Breaking eBusiness and Search News
Advertise | Newsletter | Sitemap | News Feeds News Feed 
 WebProNews Search Part of the iEntry network iEntry inc. 

Custom Tab Completion

A.P. Lawrence
Expert Author
Published: 2004-12-13

WebProNews RSS Feed


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

Compgen

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
tony


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
tony


Compgen can use the results of other commands or variables:

compgen -W '$( mount | cut -d" " -f 3)'
/
/proc
/dev/pts
/proc/bus/usb
/boot
/dev/shm


Complete

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
appl
arctic
$ compgen -A signal -- SIGA
SIGABRT
SIGALRM
$ compgen -A service -- power
powerburst
powerburst
powerguardian
powerguardian
powerclientcsf
powerclientcsf
powergemplus
powergemplus
poweroncontact
poweroncontact
poweronnud
poweronnud
powerschool
powerschool


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

#!/bin/bash
# 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":

_myprog()
{
local curw
COMPREPLY=()
curw=${COMP_WORDS[COMP_CWORD]}
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?

http://www.caliban.org/bash/#completion has some pre-made custom tab completion scripts that you can download to use or get ideas from.

* Originally published at APLawrence.com

Receive Our Daily Email of Breaking eBusiness News


About the Author:
A.P. Lawrence provides SCO Unix and Linux consulting services http://www.pcunix.com

WebProNews RSS Feed

More Articles

Contact WebProNews
Advertisement





TOP NEWS

Targeted Information for Business
WebProNews is part of the iEntry network

Internet Business: Marketing: Small Business:
WebProNews MarketingNewz SmallBusinessNewz
WebProWorld AdvertisingDay PromoteNews
EcommNewz SalesNewz EntrepreneurNewz

Software: Search Engines: Web Design:
WebMasterFree Jayde B2B DesignNewz
NetworkingFiles SearchZA FlashNewz
SecurityConfig SearchNewz WebSiteNotes

Developer: IT Management: Security:
DevWebPro ITManagement SecurityProNews
DevNewz SysAdminNews SecurityConfig
TheDevWeb NetworkingFiles NetworkNewz

The iEntry Network consists of over 100 web publications reaching millions of Internet Professionals. Contact us to advertise.
eBUSINESS RESOURCES






 Advertise | Contact Us | Corporate | Newsletter | Sitemap | Submit an Article | News Feeds
 WebProNews is an iEntry, Inc. ® publication - $line) { echo $line ; } ?> All Rights Reserved
About WebProNews
WebProNews is the number one source for eBusiness News. Over 5 million eBusiness professionals read WebProNews and other iEntry business and tech publications.

WebProNews provides real-time coverage of internet business.

Free Email Newsletters:
WebProNews SearchNewz
WebProWorld DevWebPro
Marketing SecurityNews
Plus over 100 other newsletters!

Send me relevant info on products and services.


WebProWorld
Ten most recent posts.

NetworkingFiles
Featured Software

WebProNews in the News
View all recent mentions of WebProNews from around the world!

Recent Articles On ...
Google eBusiness
Yahoo Ask Jeeves
MSN Blogs
Search Engines Blogging
Affiliate Programs Marketing
eCommerce Advertising
eBay Sun Microsystems
AOL Adsense
Microsoft Adwords
Oracle IBM
Amazon Apple
SEM Mac
SEO iPod
Adsense XBox
PR Adobe



iEntry.com WebProWorld RSS Feed WebProWorld Contact WebProNews Print Version Email a friend Bookmark us