6

Is there a way to open a GnuPG encrypted file with neovim so that one can read/edit it and on write the file is re-encrypted?

This is possible via vim-gnupg in classic vim but has not been ported to neovim. I cannot use the gpg-agent hack as I do not have X11 installed on the remote machine I wish to use this on.

5
  • As a workaround - have you tried using gpg-agent as suggested by tarruda (NeoVim author) in that thread from Github you linked?
    – grodzik
    Commented Nov 16, 2016 at 8:55
  • @dotancohen: Thank you for your bounty! Much appreciated. Commented Nov 16, 2016 at 9:05
  • @grodzik I have missed your comment when you made it: no, i cannot use it as I do not have X11 installed on the server I wish to use this on. Commented May 18, 2017 at 14:45
  • Why would you need X11 for that? if for pinentry - then there's a curses-based one as well.
    – grodzik
    Commented May 18, 2017 at 14:53
  • 2
    @grodzik Because Neovim runs external commands attached to a pipe, instead of attached to the tty as Vim did. This makes interacting with the pinentry problematic, to say the least. If you set shellpipe in Vim, you'll see similar problems. I've been contemplating whether it would be better to write a new plugin for neovim, since the current one would need a fair amount of refactoring to work nicely with neovim.
    – jamessan
    Commented May 18, 2017 at 17:19

2 Answers 2

5
+50

I did a little research and it seems that the only option right now is the one mentioned by @grodzik: you need to configure gpg-agent. Quoting Thiago de Arruda (Neovim lead developer):

I'm a long time vim-gnupg user and have been using it with neovim since day 1. I never had problems because I use gpg-agent which by defaults displays a x11 prompt to get the password (the GPG_TTY warning can be safely ignored).

In the same thread the vim-pgp author has shared his progress on making the plug-in compatible with Neovim. I think it will take a while but lately he has been contributing to Neovim (in fact the last Neovim commit is authored by him) so it's just matter of time.

1
  • I encrypted a file in vim 8.1.3741 but nvim v0.6.1 cannot open and decrypt it.
    – Stephane
    Commented Apr 4, 2022 at 12:27
0

I've been using the solution from this gist with Vim for years, so when I moved to NeoVim, I translated it to Lua, and it still works:

local gpgGroup = vim.api.nvim_create_augroup('customGpg', { clear = true })

-- autocmds execute in the order in which they were defined.
-- https://neovim.io/doc/user/autocmd.html#autocmd-define

vim.api.nvim_create_autocmd({ 'BufReadPre', 'FileReadPre' }, {
    pattern = '*.gpg',
    group = gpgGroup,
    callback = function ()
        -- Make sure nothing is written to shada file while editing an encrypted file.
        vim.opt_local.shada = nil
        -- We don't want a swap file, as it writes unencrypted data to disk
        vim.opt_local.swapfile = false
        -- Switch to binary mode to read the encrypted file
        vim.opt_local.bin = true

        vim.cmd("let ch_save = &ch|set ch=2")
    end
})

vim.api.nvim_create_autocmd({ 'BufReadPost', 'FileReadPost' }, {
    pattern = '*.gpg',
    group = gpgGroup,
    callback = function ()
        vim.cmd("'[,']!gpg --decrypt 2> /dev/null")

        -- Switch to normal mode for editing
        vim.opt_local.bin = false

        vim.cmd('let &ch = ch_save|unlet ch_save')
        vim.cmd(":doautocmd BufReadPost " .. vim.fn.expand("%:r"))
    end
})

-- Convert all text to encrypted text before writing
vim.api.nvim_create_autocmd({ 'BufWritePre', 'FileWritePre' }, {
    pattern = '*.gpg',
    group = gpgGroup,
    command = "'[,']!gpg --default-recipient-self -ae 2>/dev/null",
})
-- Undo the encryption so we are back in the normal text, directly
-- after the file has been written.
vim.api.nvim_create_autocmd({ 'BufWritePost', 'FileWritePost' }, {
    pattern = '*.gpg',
    group = gpgGroup,
    command = 'u'
})

(gist)

It is far from optimal, and I'm open to suggestions for improving it!

1
  • 1
    You might look at how zip editing works by using autocommands called (I think) BufWriteCmd and another Cmd for reading (?). That might save you some trouble.
    – D. Ben Knoble
    Commented Mar 31 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.