Converting Alpine addressbook to Mutt aliases


Having switched to a finally usable Mutt setup, I wanted to be able to use my addressbook from Alpine in Mutt. I’m also trying to learn how to use regex, sed, awk, grep etc. so I thought this was a good chance.

Alpine’s .addressbook looks like:

Aidan	Aidan Dude
Alan_H	Alan Holey		 Alans really unnecessarily long place of work
 : in a city I cantt spell 
Cameron	Cameron Green

I used a shell script like this, to manipulate this format into what Mutt likes:


address=$(cat ~/.addressbook | grep -v "^ :") 

book_length=$(echo "$address" | wc -l)

rm ~/.mutt/aliases
touch ~/.mutt/aliases

# Create alias for start of every line
alias=$(for i in `seq 1 $book_length`;
    echo 'alias '

# Get nickname from addressbook
nickname=$(echo "$address" | awk '{print $1}') 

# Get full name from addressbook
# everything before @, then before last space, then remove first word 
fullname=$(echo "$address" | grep -o '^.*\S@' | gsed  's/\S*$//g' | gsed 's/^\w*\ *//')

# Get email address from addressbook
# Get email address, then add space after comma
email=$(echo "$address" | grep -E -o "\b\S+@\S+\.[A-Za-z]{2,6}\b" | gsed 's/, */, /g')

opb=$(for i in `seq 1 $book_length`;
    echo '<'

clb=$(for i in `seq 1 $book_length`;
    echo '>'

# Paste together
paste  -d '\0' <(echo "$alias") <(echo "$nickname") <(echo "$fullname") <(echo "$opb") <(echo "$email") <(echo "$clb")  | gsed 's/\s+*/ /g' > ~/.mutt/aliases 

The outputted alias file looks like this:

alias Aidan Aidan Dude <>
alias Alan_H Alan Holey <>
alias Cameron Cameron Green <>

It also allows for multiple comma separated email addresses

I also found a way to use omnicompletion to let me search my Mutt aliases within vim when called from Mutt, so I can add them to the To: and CC: fields. The script is courtesy of [](Karsten B).