Shell tab-completion and other helpful command-line tricks

Paul Rumelhart godshatter at yahoo.com
Sat Jan 24 17:07:28 UTC 2009


Derek Broughton wrote:
> Nils Kassube wrote:
>
>   
> bash completion is fairly arcane to me, but aiui  it's up to the command's 
> packager to add some relevant file with the completions.  Everybody should 
> have apt-get - so you can try "apt-get -<tab>".  Unfortunately, apt-get's 
> completions are part of the default system, so the "apt" package doesn't 
> have a special file in it.
>
>   

I didn't know there were tab completion files involved.  That's good to 
know.

> The coolest discovery I ever made about bash completion was the fact that:
>
>   
>> scp filename  somehost::<tab>
>>     
>
> works!  It's pretty slow for the host where I usually want to use it, but so 
> handy.
>   

I've also found it works in some other programs that take input from the 
command-line, such as psql (an interface into Postgresql).  I assume 
that's built in to the psql code, and doesn't have anything to do with 
the bash shell. 

Take a look at the file /etc/bash_completion to get an idea of how many 
commands support tab completion.  I've been reading up on this, since 
you've gotten me curious.  Bash has facilities for adding custom tab 
completion to scripts you write yourself, in case you need your script 
to look at usernames or environment variables or whatever when you press 
<tab>.  Pretty cool stuff.

There is a command called "compgen" that you can call that tells you 
what will be matched for whichever program.  For example, if you want to 
find out what tab completion will find for users on your system that 
start with the letter "r", type: "compgen -A user -- r" and it should 
give you the same list that "user r<tab>" does.  This could be helpful 
inside scripts for various user inputs.  There is also a command called 
"complete" that you can use to assign tab completion to your program, 
but I haven't tried that one.  I'll have to play around with some scripts.

Paul

Paul





More information about the kubuntu-users mailing list