[ Prev ] [ Index ] [ Next ]

bash completion

Created 6/8/2007

Installing bash-completion provides access to command-line completion for basic commands like yum, p4, man?make and so on. In addition, user-defined command completions can be created quite simply. The bash-completion rpm can be installed via yum as in: bash # yum install bash-completion

image Note: This document is mostly a list of commands image Todo:

1. Optional completions

1.A. fortune completions

There are bash completions for fortune bash completions for fortune and these can be installed along with the fortune rpms via yum. The fortune completions are not (currently) avaiable in a yum repository, so download and install via rpm:

bash # wget ftp://ftp.pbone.net/mirror/kf.fyz.fce.vutbr.cz/%7Eyeti/Ftp/rpm/bash-fortunes-20040222-1.noarch.rpm
bash # rpm -Uvh bash-fortunes-20040222-1.noarch.rpm

image Note: Install the following fortune rpms to use fortune: fortune, fortune-mod and fortune-all

1.B. Color prompt

Configuring color prompts in bash using bash completion can be done by defining the following variables and functions'''

Environment variables:

PS1_USER='\e[31m'     # red user name
PS1_HOST='\e[32m'     # green machine name
PS1_PWD='\e[34m'      # blue working dir
PS1_PROMPT='\e[1m'    # bold/highlighted prompt character

Environment functions:

function coloured_prompt() {
    test "$_PS1_oldPWD" = "$PWD" -a "$_PS1_oldCOLUMNS" = "$COLUMNS" && return
    _PS1_oldPWD=$PWD
    _PS1_oldCOLUMNS=$COLUMNS

    local stop='\[\e[0m\]'
    local user='\['$PS1_USER'\]'$USER$stop
    local host='\['$PS1_HOST'\]\h'$stop
    local prompt='\['$PS1_PROMPT'\]\$'$stop
    local pwd=${PWD//#\/home\//~}
    pwd=${pwd/#~$USER/~}
    pwd=$(echo $pwd | pathabbr $[25 + ( $COLUMNS - 80 ) / 2])
    PS1=$user@$host:'\['$PS1_PWD'\]'$pwd$stop$prompt
}

Be sure that pathabbr is installed. The pathabbr app is a simple utility for crunching down the current path. It's written in c and is available as pathabbr-0.3.tar.bz2 (www.geekymedia.com). Now that we have the required apps, functions and variables, we can configure our coloured prompt by setting the bash PROMPT_COMMAND to the coloured_prompt function that we defined earlier:

bash $ PROMPT_COMMAND=coloured_prompt

1.C. Changing terminal title

To change the terminal window title, set the PS1 variable (e.g., via ~/.bashrc, (which is source'd for each new subprocess such as running a command, changing directory and so on):

[ -n "$DISPLAY" ] && { PS1='\[\e];\w ('$TERM')\007\]\w$'; } || { PS1=something_else; }

Stuart Moorfoot © 6 August 2007 foo@bund.com.au


Backlinks: :bash