Add hex editing function to vim.

This commit is contained in:
David Tomaschik
2014-09-25 22:20:59 -07:00
parent be49f3f57c
commit efec992784

41
vimrc
View File

@@ -32,3 +32,44 @@ cnoremap sudow w !sudo tee % >/dev/null
set incsearch set incsearch
set ignorecase set ignorecase
set smartcase set smartcase
" Mediocre Hex editing in vim
" Source: http://vim.wikia.com/wiki/Improved_hex_editing
nnoremap <C-H> :Hexmode<CR>
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