PreshBlog

Bash auto-completion

by on Mar.22, 2007, under Linux, System Administration

Bash allows for programmable completion, to extend the “hit tab to complete what I started typing for me” functionality.  (Programmable completion was added to Bash in 2.04 beta, so if you have a Bash older than that, it won’t work – use bash –version to find out).  Here’s a quick guide to setting up bash-completion to make Bash even nicer to use.

Install bash-completion

Firstly, install bash-completion in your normal way.  I use Arch Linux for my workstations these days, and there’s a package available, so zero effort involved:

[root@ruthenium davidp]# pacman -S bash-completion
Targets: bash-completion-20060301-6
Total Package Size:   0.1 MB
Proceed with upgrade? [Y/n]
:: Retrieving packages from extra...
bash-completion-20060301 [################] 100%      68K    79.5K/s  00:00:00
checking package integrity... done.
loading package data... done.
checking for file conflicts... done.
installing bash-completion... done.

If you’re using another distro, grab the sources or RPMs here and install in the normal way.

If you’re running under X, just opening a new terminal (xterm) generally won’t read .profile so you’ll have to manually source the new completion settings with “source /etc/bash_completion” or add a line to ~/.bashrc to do the same.

Command options auto-completion

Now, say you forget what options you can give grep.  Type “grep –” and hit tab twice, and you’ll get the list of options you can pass:

[davidp@ruthenium:~]$ grep --
--after-context=       --extended-regexp      --mmap
--basic-regexp         --file=                --no-filename
--before-context=      --files-with-matches   --no-messages
--binary               --files-without-match  --null
--binary-files=        --fixed-strings        --null-data
--byte-offset          --help                 --only-matching
--color                --ignore-case          --perl-regexp
--colour               --include=             --regexp=
--context=             --invert-match         --silent
--count                --label=               --unix-byte-offsets
--devices=             --line-buffered        --version
--directories=         --line-number          --with-filename
--exclude-from=        --line-regexp          --word-regexp
--exclude=             --max-count=
[davidp@ruthenium:~]$ grep --

Here’s a nice animated example:

grep auto-completion example animation

Finding out what commands you can use with Subversion?

[davidp@ruthenium:~]$ svn
?         cleanup   diff      log       pe        propedit  rename    switch
add       co        export    ls        pedit     propget   resolved  unlock
ann       commit    h         merge     pg        proplist  revert    up
annotate  copy      help      mkdir     pget      propset   rm        update
blame     cp        import    move      pl        ps        st
cat       del       info      mv        plist     pset      stat
checkout  delete    list      pd        praise    remove    status
ci        di        lock      pdel      propdel   ren       sw
[davidp@ruthenium:~]$ svn

Notice that it shows you the options, then gives you your prompt back with what you already typed intact, so you can just carry on typing now you know what to type :)

It’ll complete the commands for you too, so if you type “svn ch” and tab, you’ll
get “svn checkout” filled in for you (although that’s a lame example, as if you
wanted to be lazy you’d just use “svn co” (shorthand for checkout)).

SSH hostname auto-completion

A very, very useful completion is the ssh completion – it uses your known_hosts file to auto-complete hostnames, so you can type the first couple of characters of a hostname and tab to complete it.  Very natty indeed. :)

Incremental history search

Another little-used facility of Bash is the ability to search your previous command istory for a command to rerun.  Just hit Ctrl+r and start typing, and it’ll display whatever matches.  Ctrl+r again will select other matching commands, and enter will un it again.

I tried out bash-completion after reading a blog post on linuxhelp.blogspot.com.

There’s plenty more useful information about working productively with Bash on  http://www.caliban.org/bash/ too.


4 Comments for this entry

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!