Mutt, Vim, and a win!
Today, after dealing with a segfault, I started playing with mutt and so far I'm really liking it. For example, if you have a gmail account there exists a LOT of different tutorials on the web about connecting, and if you use another service, there's a high chance that google or duckduckgo will find it for you. So I won't be covering how to connect mutt to servers in this post. Instead, I'd like to explain why I picked up mutt in the first place. And a useful trick for you if you use vim as your editor of choice.
When I send pgp encrypted emails, I have to do something like the following:
-
Open vim
-
write message
-
open terminal (not hard, I have quake so it's just one button)
-
cat file | gpg --encrypt --armor -r <recipient>
-
copy output
-
open email client
-
paste pgp message
-
enter recipients for message
-
send
That's 9 steps to do something that should be simple!
Well, after installing and configuring mutt I can now skip the last 4 steps, which means now I'm left with the other 5. Since mutt will handle opening vim for us, we'll actually end up with 4 steps.
-
start mutt
-
compose message
-
???
-
send
The magical ??? is what we'll explore next. I was trying to figure out how I could send whatever was in my txt file to gpg when I happened upon this page and found this useful note:
In visual mode, just highlight the text you want to work with, then run :! commandname .
So if I'm in visual mode I can simply type my gpg encryption command and
the message will get automatically replaced with the encrypted text. This
is all good and great, but the only part of the command that changes is
the recipients. So writing the --encrypt --armor
all the time is a bit
redundant to me, and programmers are lazy, so I decided to finally look
up how to write a vim macro.
Perhaps the strangest thing while writing the macro was trying to determine how to STOP recording while still in the middle of a command in vim. Sadly, I didn't figure it out (I'm sure I'm missing a manual somewhere), but I did figure out a way to remove the esc + q keys I had to press to get out of it. Which is essentially the same thing. Doing this let's us fill in the other options we want to pass to gpg.
In order to do this yourself follow these steps:
-
open anything up in vim, or make a new file and write make a few lines of text.
-
press
q
thene
to begin recording a macro into the e register. -
press
:0
to jump to the top of the file -
type
shift+v
to enter visual mode -
type
shift+g
to jump to the bottom of the file -
type
:!gpg --encrypt --armor -r
-
press
ESC
, thenq
thene
to finish recording -
Next, enter command mode and type
let @e='
then pressCtrl+r Ctrl+r e
-
This should paste the full register into the space, and you'll notice some trailing mess at the end like
^]
-
Delete said mess, leaving a space after
-r
and put in the last apostrophe and hit enter. -
you can now press
@e
at any time to open up the prompt to enter recipients and encrypt the whole file.
Or, the TL;DR version, add this to your .vimrc file:
let @e=':0^MVG:!gpg --encrypt --armor -r '
Note that when you enter the ^M, you'll want to use Ctrl+v
then press the enter
key otherwise the command will not work!
So now we have a method to easily encrypt the body of an email message. What about decrypting? If you'd like to record this one yourself, you can do the following steps or skip down to the .vimrc command:
-
press
q
thend
to start recording into the d register -
/-----BEGIN PGP MESSAGE
-
shift + v
to enter visual mode -
/END PGP MESSAGE-----
-
:!gpg --decrypt
his enter to decrypt, thenESC
andd
to end the recording
All in all, add this to the .vimrc file
let @d='/-----BEGIN PGP MESSAGE^MV/END PGP MESSAGE-----^M:!gpg --decrypt^M'
again, making sure to use ctrl+v
then the enter key to add the ^M
parts.
Now you can encrypt and decrypt extremely easily! If you're interested in reading more about vim's commands, I'd recommend the documentation. It's quite easy to follow, and if you want to know specifically about macros, you should read the wiki page on it, it's got a lot of good information.