An introduction to bash completion: part 1

Posted by Steve on Fri 23 Dec 2005 at 09:34

One of the nicest facilities of the modern shell is the built in "completion" support. These facilities allow you to complete commands and their arguments easily. Read on for a brief introduction to adding your own command completions.

Most shells allow command completion, typically bound to the TAB key, which allow you to complete the names of commands stored upon your PATH, file names, or directory names. This is typically used like so:

ls /bo[TAB]

When you press the TAB key the argument /bo is automatically replaced with the value /boot.

Recently some shells have started allowing you to do even more: completing arguments to commands. Two notable shells which allow this are zsh, and bash. Since I'm a bash user I'm only going to cover that.

The Debian bash package supplies a command line completion file /etc/bash_completion which sets up some common support.

If you're not using it right now you can load it by typing into your shell ". /etc/bash_completion" as shown here:

skx@lappy:~$ . /etc/bash_completion
skx@lappy:~$ 

Once this is done you'll be able to TAB-complete many common arguments to programs, for example:

skx@lappy:~$ apt-get upd[TAB]
skx@lappy:~$ apt-get upg[TAB]

But how do you extend the support yourself? Well the completion routines supplied make use of several internal bash commands such as complete. These can be used by your own shell startup files, or more easily by creating a small file and dropping it into the directory /etc/bash_completion.d/.

When the bash_completion file is sourced (or loaded) everything inside the /etc/bash_completion.d directory is also loaded. This makes it a simple matter to add your own hooks.

One of the things which bash allows you to complete is hostnames, this can be very useful for some commands.

I remotely manage some computers using VNC and I usually do that by running the command "xvncviewer hostname".

To allow bash to complete the hostname fragment I type with we'll use the complete command to tell it that xvncviewer requires a hostname:

skx@lappy:~$ complete -F _known_hosts xvncviewer

Once I've done this I can type [TAB] to complete hostnames:

skx@lappy:~$ xvncviewer s[TAB]
savannah.gnu.org            ssh.tardis.ed.ac.uk
scratchy                    steve.org.uk
security.debian.org         security-master.debian.org
sun
skx@lappy:~$ xvncviewer sc[TAB]

This has now completed the hostname scratchy for me.

The function _known_hosts is defined in the file /etc/bash_completion. How did I know I could use it? By using the command "complete -p" to display all of the bindings in use:

skx@lappy:~$ complete -p
....
complete -F _known_hosts tracepath6
complete -F _known_hosts host
...

So what have we learnt so far?

  • Command line completion exists.
  • Completion is implemented in the file /etc/bash_completion
  • New completion commands may be placed inside the directory /etc/bash_completion.d
  • We can list all the current completion routines bound via "complete -p"

In part two we'll look at defining custom command line handling routines - similar to those already in place. So we can add command line completion to our own programs, or commands not yet covered.

Until then you might want to experiment a little yourself.

 

 


Posted by kamaraju (24.58.xx.xx) on Sat 24 Dec 2005 at 05:06
A very nice and informative article. Thanks. Now I should go back and read the part2.

[ Parent | Reply to this comment ]

Posted by Steve (82.41.xx.xx) on Sat 24 Dec 2005 at 19:08
[ View Steve's Scratchpad | View Weblogs ]

Thanks :)

I almost joined the two pieces together, but it seemed nicer to seperate them out into distinct pieces.

Steve

[ Parent | Reply to this comment ]

Posted by yeti (84.133.xx.xx) on Sat 24 Dec 2005 at 20:47
[ View yeti's Scratchpad ]
Sometimes this luxury drives me crazy...

If I have a tar file not named with suffix .tar and I type "tar xvf filenamebeginning<tab>", what will happen?

Same effect for many other extension driven suggestions or completions...

It is very usefull to know, that if I type "\tar" instead of "tar", command completion will not happen at all. Even if "tar was aliased, "\tar" will override this IIRC...

But now for something (not really) completely different...

Another nice frature for small functions or big screens is the way functions are "completed". Try "function _mc<tab><tab>". You can edit the function with bash's commandline editor and even rename the function name.

Similar for aliases. Assume having aliased "ll" to somethig obvious then "alias ll=<tab>" gives the alias definition als editable line...

[ Parent | Reply to this comment ]

Posted by rompe (194.175.xx.xx) on Thu 26 Jan 2006 at 16:21
If I have a tar file not named with suffix .tar and I type "tar xvf filenamebeginning", what will happen?
You will try Shift-TAB instead of TAB to revert to standard filename expansion.

[x] ulf

[ Parent | Reply to this comment ]

Posted by Anonymous (83.240.xx.xx) on Sun 4 Mar 2007 at 16:20
The shift-TAB doesn't work for me, to revert to standard filename expansion (it just does nothing).

[ Parent | Reply to this comment ]

Posted by Anonymous (83.240.xx.xx) on Sun 4 Mar 2007 at 16:27
The solution is to use ESC-TAB. This works.

[ Parent | Reply to this comment ]

Posted by Anonymous (90.1.xx.xx) on Fri 2 Nov 2007 at 21:23
Only Alt-/ works for me

[ Parent | Reply to this comment ]

Posted by Anonymous (82.16.xx.xx) on Wed 5 Nov 2008 at 10:50
A slight update to this article: bash-completion is now a separate Debian package.

[ Parent | Reply to this comment ]

Posted by Anonymous (203.206.xx.xx) on Wed 19 May 2010 at 04:51
Anyone interested, here's my clearcase completion script for bash: http://jan.tomka.name/project/clearcase-completion-bash

[ Parent | Reply to this comment ]

Posted by Jovana (178.222.xx.xx) on Fri 19 Oct 2012 at 10:55
This article is translated to Serbo-Croatian language by Jovana Milutinovich from Geeks Education.

[ Parent | Reply to this comment ]

Sign In

Username:

Password:

[Register|Advanced]

 

Flattr

 

Current Poll

What do you use for configuration management?








( 677 votes ~ 10 comments )

 

 

Related Links