Getting bash Completion Magic on OS X

Paul R. Brown @ 2008-06-25T07:18:06Z

One of the many nifty features of bash is that it provides context-sensitive completion, but for some reason this capability isn't part of the bash that ships with Mac OS X, at least as of 10.5.3, which is what I'm presently using.

To rectify the oversight, first install the bash_completion port via MacPorts:

$ sudo port install bash-completion

And then modify your ~/.profile as directed, e.g., by adding:

if [ -f /opt/local/etc/bash_completion ]; then
    . /opt/local/etc/bash_completion
fi

To load your own local collection of completion hooks, create the directory ~/.bash_completion.d and then put the following in ~/.bash_completion (essentially cut/pasted out of /opt/local/etc/bash_completion):

if [ -d $USER_BASH_COMPLETION_DIR -a -r $USER_BASH_COMPLETION_DIR -a \
     -x $USER_BASH_COMPLETION_DIR ]; then
        for i in $USER_BASH_COMPLETION_DIR/*; do
                [[ ${i##*/} != @(*~|*.bak|*.swp|\#*\#|*.dpkg*|.rpm*) ]] &&
                        [ \( -f $i -o -h $i \) -a -r $i ] && . $i
        done
fi
unset i

Next, above the other block added to ~/.profile, add:

export USER_BASH_COMPLETION_DIR=~/.bash_completion.d

As for the contents of .bash_completion.d, I put the completion files supplied with darcs and cabal-install there, so in addition to the usual niceties like completion of paths on remote servers (e.g., for scp), I also get convenient completion behavior at the commandline, e.g., with cabal:

$ cabal install hsc[TAB]
hsc2hs hsc3 hsclock hscolour hscurses
(comment bubbles) 0 comments

bash Tidbit of the Day

Paul Brown @ 2007-09-22T22:19:00Z

Along with !! (last command) and !$ (last argument), I can't remember not knowing the ^ quick substitution feature in bash for replacing a string in the last entry:

$ echo foo
foo
$ ^foo^bar
echo bar
bar

But that doesn't handle multiple occurrences:

$ echo foo foo
foo foo
$ ^foo^bar
echo bar foo
bar foo

I read the fine manual looking for something else and found the !!:gs/old/new/ history expansion feature:

$ echo foo foo foo
foo foo foo 
$ !!:gs/foo/bar/
echo bar bar bar
bar bar bar
(comment bubbles) 1 comment

The "bad interpreter" Bad Error Message

Paul Brown @ 2006-11-26T14:44:00Z

Every so often, I try to run a shell script and get the otherwise unhelpful:

: bad interpreter: No such file or directory

It always takes me a few minutes to remember that this is from MS-DOS line endings, and this is the message that there's no interpreter named /bin/bash^M. The Emacs quickie to clean out the line endings is:

C-x [ENTER] f unix

From the commandline,

cat -v file

will show the ^M, and

cat filename | tr -d '\r' > newfile

will remove them.

(comment bubbles) 0 comments