Last active
February 23, 2016 20:53
-
-
Save d0c-s4vage/e03cbe2380f751b9ec1f to your computer and use it in GitHub Desktop.
Simple vim script to define transforms for specific file types, be it gpg encryption, aes256 with openssl, base64, etc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scriptencoding utf-8 | |
function! PrepareTrans() | |
setl viminfo= | |
setl noswapfile | |
if exists("+undofile") | |
setl noundofile | |
endif | |
setl noeol | |
setl binary | |
endfunction | |
function! PrepareEdit() | |
setl nobinary | |
setl eol | |
endfunction | |
function! DoTrans(trans_cmd) | |
call PrepareTrans() | |
" save our current location in the file in the p register | |
normal mp | |
silent! execute "%!bash -lc '".a:trans_cmd."'" | |
endfunction | |
function! PostTrans() | |
" undo to get back the non-transformed data | |
u | |
" jump back to our mark | |
normal `p | |
endfunction | |
function! DoTransRestore(restore_cmd) | |
silent! execute "%!bash -lc '".a:restore_cmd."'" | |
call PrepareEdit() | |
endfunction | |
function! CreateTransform(filetypes, trans_cmd, restore_cmd) | |
execute "autocmd BufReadPre,FileReadPre ".a:filetypes." call PrepareTrans()" | |
execute "autocmd BufReadPost,FileReadPost ".a:filetypes." call DoTransRestore('".a:restore_cmd."')" | |
execute "autocmd BufWritePre,FileWritePre ".a:filetypes." call DoTrans('".a:trans_cmd."')" | |
execute "autocmd BufWritePost,FileWritePost ".a:filetypes." call PostTrans()" | |
endfunction | |
" --------------------------- | |
augroup transforms | |
au! | |
" custom | |
let aes_256_encrypt = 'openssl enc -e -aes256 -k "$AES_SECRET"' | |
let aes_256_decrypt = 'openssl enc -d -aes256 -k "$AES_SECRET"' | |
call CreateTransform("*.enc", aes_256_encrypt, aes_256_decrypt) | |
" binary gpg encryption | |
let gpg_encrypt = "gpg --batch --encrypt --recipient [email protected] 2>/dev/null" | |
let gpg_decrypt = "gpg --batch --decrypt 2>/dev/null" | |
call CreateTransform("*.gpg", gpg_encrypt, gpg_decrypt) | |
" armored gpg encryption | |
let gpg_encrypt_asc = "gpg --batch --encrypt --recipient [email protected] --armor 2>/dev/null" | |
let gpg_decrypt_asc = "gpg --batch --decrypt 2>/dev/null" | |
call CreateTransform("*.asc", gpg_encrypt_asc, gpg_decrypt_asc) | |
" base64 | |
call CreateTransform("*.b64", "base64", "base64 -d") | |
augroup end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment