From efec992784f5626ec2647c48af3fb9b52253d0e5 Mon Sep 17 00:00:00 2001 From: David Tomaschik Date: Thu, 25 Sep 2014 22:20:59 -0700 Subject: [PATCH] Add hex editing function to vim. --- vimrc | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/vimrc b/vimrc index 532370c..cc3ac9a 100644 --- a/vimrc +++ b/vimrc @@ -32,3 +32,44 @@ cnoremap sudow w !sudo tee % >/dev/null set incsearch set ignorecase set smartcase + +" Mediocre Hex editing in vim +" Source: http://vim.wikia.com/wiki/Improved_hex_editing +nnoremap :Hexmode +command -bar Hexmode call ToggleHex() +function ToggleHex() + " hex mode should be considered a read-only operation + " save values for modified and read-only for restoration later, + " and clear the read-only flag for now + let l:modified=&mod + let l:oldreadonly=&readonly + let &readonly=0 + let l:oldmodifiable=&modifiable + let &modifiable=1 + if !exists("b:editHex") || !b:editHex + " save old options + let b:oldft=&ft + let b:oldbin=&bin + " set new options + setlocal binary " make sure it overrides any textwidth, etc. + let &ft="xxd" + " set status + let b:editHex=1 + " switch to hex editor + %!xxd + else + " restore old options + let &ft=b:oldft + if !b:oldbin + setlocal nobinary + endif + " set status + let b:editHex=0 + " return to normal editing + %!xxd -r + endif + " restore values for modified and read only state + let &mod=l:modified + let &readonly=l:oldreadonly + let &modifiable=l:oldmodifiable +endfunction