Google contacts in mutt and vim

I’m a long-time fan of the mutt email client. I’ve probably been using it for ten years. It’s quick, text-based, and does precisely what I want. I’ve been using the vim text editor for even longer than that.

In using mutt and vim, though, I surrender some of the convenience of a mail client like Thunderbird, Mail.app or Outlook, which are fully integrated with contacts and calendars. Fortunately, mutt and vim make it easy to solve my own problem.

Finding Contacts

First, install goobook. It’s a python script that lets you easily query Google Contacts. Goobook returns results in abook format, which is great if you’re using that companion utility for mutt to provide email addresses to mutt before you draft your email. I prefer to just hit compose and get a blank email and fill in the To:, Cc:, etc. myself.

So what I want is to hit a key while I’m editing a message, have vim read the word that I’m on, and autocomplete that word based on my Google Contacts. First order of business is fixing the output of Goobook to something useful, which I do with this little script:

#!/bin/sh

goobook query "$*" | sed -e '/^$/d' -e 's/(.*) (.*)t.*/1 2/' -e 's/(^<.*)t(.*)/"2" <1>/'

The first sed line cleans up “group” entries, the second formats each entry to be RFC 822-ready, like god intended. I call the script google-contacts-lookup.sh, and put it in my ~/bin directory.

Then, I tell vim where to find it, with this line in my .vimrc:


imap <C-F> <ESC>:r!google-contacts-lookup.sh <cword><CR><ESC>

With goobook installed, google-contacts-lookup.sh in my path, and that line in my .vimrc, I can type “Gunnar”, hit Control-F, and automatically have all the matching email addresses appear below my cursor. Sweet!

Adding Contacts

So when I get an email from someone I don’t know, I want to be able to add their address to Google Contacts. goobook makes this pretty easy. I just highlight the message in question, and “| goobook add” and the email address in the “From:” field is imported. Strong.

Ideally, I’d be able to send a vCard or a blob of text from an email, like a signature, have it parsed for emails, phone numbers, and so on, and have than sent up to Google Contacts. But that’s for another day.

2 thoughts on “Google contacts in mutt and vim

  1. in your .vimrc is says google-contacts-lookup.py, but your file is .sh , is that correct?

    Like

    1. You’re right — that’s a typo. Sorry about that. It should be google-contacts-lookup.sh instead.

      Like

Comments are closed.