Bill Jonas on Tue, 31 Jul 2001 21:02:50 -0400


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Have Vim transparently handle encrypted files


Okay, Darxus pointed out a small problem with what I posted.  To correct
it, add a single line containing just "endif" after what I already
posted.
 
Another note: The vim command sequence assumes an extension of .gpg for
data in binary format and .asc for ASCII-armored encrypted stuff.  If
you want to set it to something different, just
":%s/\*\.gpg/*.extensionyouwant/" and ":%s/\*\.asc/*.extensionyouwant/".
This doesn't make a difference for decrypting, just for encrypting.
(I'm sure there's a way to do it more efficiently than copying all the
lines over again, but this was a five-minute hackjob.)
 
Oh, and there was another problem with a plaintext copy of the file
being left lying around.  I fixed the problem, too.  I'm attaching a
copy of the corrected version.

-- 
Bill Jonas    *    bill@billjonas.com    *    http://www.billjonas.com/
"As we enjoy great advantages from the inventions of others,  we should
be glad of an opportunity to serve others by any invention of ours; and
this we should do freely and generously."          -- Benjamin Franklin
if has("autocmd")

" Support editing of gpg-encrypted files
augroup gnupg
  " Remove all gnupg autocommands
  au!

  " Enable editing of gpg-encrypted files
  "	  read:	set binary mode before reading the file
  "		decrypt text in buffer after reading
  "	 write:	encrypt file after writing
  "	append:	decrypt file, append, encrypt file
  autocmd BufReadPre,FileReadPre	*.gpg set bin
  autocmd BufReadPre,FileReadPre	*.gpg let ch_save = &ch|set ch=2
  autocmd BufReadPost,FileReadPost	*.gpg '[,']!gpg -d 2>/dev/null
  autocmd BufReadPost,FileReadPost	*.gpg set nobin
  autocmd BufReadPost,FileReadPost	*.gpg let &ch = ch_save|unlet ch_save
  autocmd BufReadPost,FileReadPost	*.gpg execute ":doautocmd BufReadPost " . expand("%:r")

  autocmd BufWritePost,FileWritePost	*.gpg !mv <afile> <afile>:r
  autocmd BufWritePost,FileWritePost	*.gpg !gpg -e <afile>:r
  autocmd BufWritePost,FileWritePost	*.gpg !rm <afile>:r

  autocmd FileAppendPre			*.gpg !gpg -d 2>/dev/null <afile>
  autocmd FileAppendPre			*.gpg !mv <afile>:r <afile>
  autocmd FileAppendPost		*.gpg !mv <afile> <afile>:r
  autocmd FileAppendPost		*.gpg !gpg -e <afile>:r
  autocmd FileAppendPost		*.gpg !rm <afile>:r

  " Same as above, but for ASCII-armored files
  autocmd BufReadPre,FileReadPre	*.asc set bin
  autocmd BufReadPre,FileReadPre	*.asc let ch_save = &ch|set ch=2
  autocmd BufReadPost,FileReadPost	*.asc '[,']!gpg -d 2>/dev/null
  autocmd BufReadPost,FileReadPost	*.asc set nobin
  autocmd BufReadPost,FileReadPost	*.asc let &ch = ch_save|unlet ch_save
  autocmd BufReadPost,FileReadPost	*.asc execute ":doautocmd BufReadPost " . expand("%:r")

  autocmd BufWritePost,FileWritePost	*.asc !mv <afile> <afile>:r
  autocmd BufWritePost,FileWritePost	*.asc !gpg -a -e <afile>:r
  autocmd BufWritePost,FileWritePost	*.asc !rm <afile>:r

  autocmd FileAppendPre			*.asc !gpg -d 2>/dev/null <afile>
  autocmd FileAppendPre			*.asc !mv <afile>:r <afile>
  autocmd FileAppendPost		*.asc !mv <afile> <afile>:r
  autocmd FileAppendPost		*.asc !gpg -a -e <afile>:r
  autocmd FileAppendPost		*.asc !rm <afile>:r
augroup END

endif