Transparent editing of GPG encrypted files with Vim

transparent editing of GPG encrypted files with Vim
transparent editing of GPG encrypted files with Vim

Do you want to edit gpg encrypted data and like Vim? This post is about how to combine both, GPG and Vim.

In case vim is not installed yet, you can install vim e.g. on Ubuntu with

sudo apt-get install vim

In case gpg (gnupg) is not installed yet, you can install it e.g. on Ubuntu:

sudo apt-get install gnupg gnupg2
alias gpg="gpg2"

Now, GPG (gnupg) can be connected with vim as plugin.

In case the required vim plugin folders do not exist yet, create those e.g. on Ubuntu

mkdir -p ~/.vim/plugin/

and download the plugin

cd ~/.vim
git clone git@github.com:jamessan/vim-gnupg.git
mv vim-gnupg/* .
chmod +x plugin/gnupg.vim

Create a local gpg key now.

The next steps is to set the regarding environment variables e.g. for bash:

vim ~/.bashrc
# 'Shift + G' to got to end of the file 
# 'o' to switch to insert mode in a new line
export GPG_TTY=`tty`
# ESC and ':x' to write changes to '~/.bashrc' file and close

You can set plugin defaults in ~/.vimrc:

let g:GPGPreferArmor=1
let g:GPGDefaultRecipients=["yourname@yourdomain.com"]

This settings prefers ASCII-armored files, and sets your default recipient list.

All preparation is done, let’s test it:

vim test.asc

In my case I get:

GPG: ----------------------------------------------------------------------
GPG: Please edit the list of recipients, one recipient per line.
GPG: Unknown recipients have a prepended "!".
GPG: Lines beginning with "GPG:" are removed automatically.
GPG: Data after recipients between and including "(" and ")" is ignored.
GPG: Closing this buffer commits changes.
GPG: ----------------------------------------------------------------------
[your name used in gpg key] <[your email used in gpg key]> (optional: e.g. ID: ... created at ...)

Save this with ‘:x’.

Now, you can add your data e.g.

# 'i' to switch to insert mode
My encrypted data.
# 'ESC' to leave insert mode

Save now with ‘:x’.

Lets lets see how the file test.asc looks like:

$ cat test.asc 
-----BEGIN PGP MESSAGE-----

hQGMA32E2byu+6wsAQv5AYJr5Q7cGWV7eCG4qTsgStA1cS7hb6H0pnDfVk50BMCx
G01baYCaz6H8jEcqsvP3Z2OwMgirXU1Fdp66XxcWY/tBy0FZZYDdvrkY3Lz0E9V7
KlV739/zbRFg0P/Vl5qgqU796K/RWBohnFmBmXlJbV92Y2SD55omzCY4NooY9Kwc
eciSreUMVQKMN8LCxdulUQ3hMA+Qt7dc7oXb1KdecmkYIuj0ZQCEJSvzG7H52S6h
deN1S+tXROvgKO2Yk+djYnLHnOD906lg2xSPtunctq1hbkFgcTpjfQ3lCEZ3xoCH
hVYS76R9abKjjhbk5DF/7+/wGruxEEUtakXrru4tpWvNwglptkpO5hMvgzpmV+/A
M5Hi68yIWUMdooJtIXEws+V1hTlG1+oFd8ENkdd+EYM7wEx0lfX80oG65c/xY1M7
FgtE/Es+u67qB/mlcomfmE58MR/yBb0W0tcmzD5EvCzS6qcIkvbiWzg7OJL2QdVx
wzppkRXNwEBs3iEa7G9W0k4BfdKvrBd5ncF6j7hjJbv8YTywfZ4ocOtM7ivt2UZU
+ItROiUo+5FzsJ+k2n0UCldlOjAV6r5Y18O0VssGmrO55m3hKPPemgab2bWWD3Q=
=9CWz
-----END PGP MESSAGE-----
$ gpg -d test.asc
gpg: encrypted with 3072-bit RSA key, ID [.....], ......
      "Lothar Schulz <[email]>"
My encrypted data.

Conclusion

Vim, gpg (gnupg) and the vim gnupg plugin make transparent editing of GPG encrypted files with Vim easy and fun!


default encryption settings for initial test.asc file
default encryption settings for initial test.asc file
test.asc file with sample content
test.asc file with sample content
test.asc file in clear text and encrypted

update 2024 03 25:

updated with information of https://github.com/jamessan/vim-gnupg/issues/102#issuecomment-502409404

4 Comments

  1. This isn’t working for me.

    After downloading the plugin .vim file, and updating `bashrc` and `vimrc`, I get the following when trying to open a new `foo.asc` file:

    “`
    Error detected while processing BufReadCmd Autocommands for “*.{gpg,asc,pgp}”:
    E117: Unknown function: gnupg#init
    “`

    What did I do wrong?

    • Sorry to read you had trouble following the steps.

      The error message you reported indicates the root cause might be in gnupg.vim#L36.
      My understanding of this code line is: gnupg’s init function is called.
      This init function does probably not exist or is configured differently then expected in the system you use. I ask you to verify your gnupg terminal setup e.g. with
      $ gpg --version

    • Try this MC, worked for me

      $ mkdir -p ~/.vim/pack/bundle/start
      $ cd ~/.vim/pack/bundle/start
      $ git clone https://github.com/jamessan/vim-gnupg.git

      $ vim test.asc

  2. Try this MC, worked for me

    $ mkdir -p ~/.vim/pack/bundle/start
    $ cd ~/.vim/pack/bundle/start
    $ git clone git@github.com:jamessan/vim-gnupg.git

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.