Skip to content

Instantly share code, notes, and snippets.

@traut
Last active July 4, 2024 09:46
Show Gist options
  • Save traut/cd19ae2817ab13e0bade1f8a9995029f to your computer and use it in GitHub Desktop.
Save traut/cd19ae2817ab13e0bade1f8a9995029f to your computer and use it in GitHub Desktop.
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'
})
@nickali
Copy link

nickali commented Jul 2, 2024

When I open a .gpg file, It displays the contents of file unencrypted as expected, but at the bottom it says 'Press ENTER or type command to continue'. If you hit ENTER or press any other key, I see the following error:

Error detected while processing BufReadPost Autocommands for "*.gpg":
Error executing lua callback: vim/_editor.lua:0: BufReadPost Autocommands for "*.gpg"..script nvim_exec2() called at BufReadPost Autocommands for "*.gpg":0: Vim(let):E121: Undefined variable: ch_save
stack traceback:
        [C]: in function 'nvim_exec2'
        vim/_editor.lua: in function 'cmd'
        /Users/me/.config/nvim/lua/plugins/decode-gpg.lua:30: in function </Users/me/.config/nvim/lua/plugins/decode-gpg.lua:24>
        [C]: in function 'nvim_exec_autocmds'
        .../AstroNvim/lua/astronvim/plugins/_astrocore_autocmds.lua:171: in function <.../AstroNvim/lua/astronvim/plugins/_astrocore_autocmds.lua:169>

Then if you press any key, the text is cleared out.

EDIT: I made some small changes to make it work for me: https://gist.github.com/nickali/89f3743e305db015d0f3ad4ffd325ccb.

@traut
Copy link
Author

traut commented Jul 4, 2024

awesome, thank you @nickali

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment