Bill Jonas on Tue, 31 Jul 2001 19:41:14 -0400


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

[PLUG] Have Vim transparently handle encrypted files


You know how if Vim is properly configured it will let you edit gzip'd
and bzip'd files transparently?  Well, Darxus gave me the idea from
talking in #plug about having it handle encrypted files the same way.
So I spent a few minutes hacking up the gzip example that was already
there.  I thought I should share the result.

Here's what I came up with.  Any comments, suggestions?

----Cut here----
if has("autocmd")

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

  " Enable editing of gpg-encrypted files
  " Inspired from the gzip section of Debian's /etc/vimrc
  " By Bill Jonas, bill@billjonas.com
  "	  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 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

  " 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 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
augroup END
----Cut here----

-- 
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


______________________________________________________________________
Philadelphia Linux Users Group       -      http://www.phillylinux.org
Announcements-http://lists.phillylinux.org/mail/listinfo/plug-announce
General Discussion  -  http://lists.phillylinux.org/mail/listinfo/plug