8.2. Encrypted Mail with vim

Problem

You want to compose an encrypted mail message, and your mail editor is vim.

Solution

               ~/.vimrc:
map ^E :1,$!gpg --armor --encrypt 2>/dev/null^M^L
map ^G :1,$!gpg --armor --encrypt --sign 2>/dev/null^M^L
map ^Y :1,$!gpg --clearsign 2>/dev/null^M^L

Tip

The ^X symbols are actual control characters inserted into the file, not a caret followed by a letter. In vim, this is accomplished by pressing ctrl-V followed by the desired key, for example, ctrl-V ctrl-E to insert a ctrl-E.

Discussion

These macros filter the entire edit buffer (1,$) through gpg. The first macro merely encrypts the buffer, the second encrypts and signs, and the third only signs. You’ll be prompted for your passphrase for any signing.

See Also

gpg(1), vim(1). Credit goes to Rick van Rein for this tip: http://rick.vanrein.org/linux/tricks/elmPGP.html.

Get Linux Security Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.