small pixel drawing of a pufferfish dotfiles

add transparent editing of age files
Sat, 23 Jan 2021 02:00:14 -0600




1 files changed, 32 insertions(+), 0 deletions(-)

jump to
M .vimrc.vimrc

@@ -381,3 +381,35 @@

let @/ = l:pattern let @" = l:saved_reg endfunction + +" Transparent editing of age encrypted files. +augroup encrypted +au! +" First make sure nothing is written to ~/.viminfo while editing +" an encrypted file. +autocmd BufReadPre,FileReadPre *.age set viminfo= +" We don't want a swap file, as it writes unencrypted data to disk +autocmd BufReadPre,FileReadPre *.age set noswapfile +" Switch to binary mode to read the encrypted file +autocmd BufReadPre,FileReadPre *.age set bin +autocmd BufReadPre,FileReadPre *.age let ch_save = &ch|set ch=2 +autocmd BufReadPre,FileReadPre *.age let shsave=&sh +autocmd BufReadPre,FileReadPre *.age let &sh='sh' +autocmd BufReadPre,FileReadPre *.age let ch_save = &ch|set ch=2 +autocmd BufReadPost,FileReadPost *.age '[,']!age -i ~/.age/key.txt --decrypt 2> /dev/null +autocmd BufReadPost,FileReadPost *.age let &sh=shsave +" Switch to normal mode for editing +autocmd BufReadPost,FileReadPost *.age set nobin +autocmd BufReadPost,FileReadPost *.age let &ch = ch_save|unlet ch_save +autocmd BufReadPost,FileReadPost *.age execute ":doautocmd BufReadPost " . expand("%:r") +" Convert all text to encrypted text before writing +autocmd BufWritePre,FileWritePre *.age set bin +autocmd BufWritePre,FileWritePre *.age let shsave=&sh +autocmd BufWritePre,FileWritePre *.age let &sh='sh' +autocmd BufWritePre,FileWritePre *.age '[,']!age -r "$(sed -n 's/.*\(age\)/\1/p' ~/.age/key.txt)" 2>/dev/null +autocmd BufWritePre,FileWritePre *.age let &sh=shsave +" Undo the encryption so we are back in the normal text, directly +" after the file has been written. +autocmd BufWritePost,FileWritePost *.age silent u +autocmd BufWritePost,FileWritePost *.age set nobin +augroup END