Converting Alpine addressbook to Mutt aliases

2018-12-23

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	aidan.dude@email.com
Alan_H	Alan Holey	alan.holey@email.com		 Alans really unnecessarily long place of work
 : in a city I cantt spell 
Cameron	Cameron Green	cameron@gmail.com

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

#!/bin/bash

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`;
do 
    echo 'alias '
done)

# 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`;
do 
    echo '<'
done)

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

# 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 <aidan.dude@email.com>
alias Alan_H Alan Holey <alan.holey@email.com>
alias Cameron Cameron Green <cameron@gmail.com>

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 [https://www.vim.org/scripts/script.php?script_id=2533](Karsten B).