Vim Tips Wiki
(New page: The help files for vim include the following advice for automating the xxd-style hex editing capabilities for vim: " vim -b : edit binary using xxd-format! augroup Binary au! au B...)
 
(fixing code section format)
Line 1: Line 1:
 
The help files for vim include the following advice for automating the xxd-style hex editing capabilities for vim:
 
The help files for vim include the following advice for automating the xxd-style hex editing capabilities for vim:
   
" vim -b : edit binary using xxd-format!
+
" vim -b : edit binary using xxd-format!
augroup Binary
+
augroup Binary
au!
+
au!
au BufReadPre *.bin let &bin=1
+
au BufReadPre *.bin let &bin=1
au BufReadPost *.bin if &bin | %!xxd
+
au BufReadPost *.bin if &bin | %!xxd
au BufReadPost *.bin set ft=xxd | endif
+
au BufReadPost *.bin set ft=xxd | endif
au BufWritePre *.bin if &bin | %!xxd -r
+
au BufWritePre *.bin if &bin | %!xxd -r
au BufWritePre *.bin endif
+
au BufWritePre *.bin endif
au BufWritePost *.bin if &bin | %!xxd
+
au BufWritePost *.bin if &bin | %!xxd
au BufWritePost *.bin set nomod | endif
+
au BufWritePost *.bin set nomod | endif
augroup END
+
augroup END
   
 
There are a few problems with this approach:
 
There are a few problems with this approach:
Line 22: Line 22:
 
Put the following in your vimrc file:
 
Put the following in your vimrc file:
   
if has("autocmd")
+
if has("autocmd")
" vim -b : edit binary using xxd-format!
+
" vim -b : edit binary using xxd-format!
augroup Binary
+
augroup Binary
au!
+
au!
au BufReadPre *.bin,*.hex setlocal binary
+
au BufReadPre *.bin,*.hex setlocal binary
au BufReadPost *
+
au BufReadPost *
\ if &binary | exe "Hexmode" | endif
+
\ if &binary | exe "Hexmode" | endif
au BufWritePre *
+
au BufWritePre *
\ if exists("b:editHex") && b:editHex && &binary |
+
\ if exists("b:editHex") && b:editHex && &binary |
\ exe "%!xxd -r" |
+
\ exe "%!xxd -r" |
\ endif
+
\ endif
au BufWritePost *
+
au BufWritePost *
\ if exists("b:editHex") && b:editHex && &binary |
+
\ if exists("b:editHex") && b:editHex && &binary |
\ exe "%!xxd" |
+
\ exe "%!xxd" |
\ exe "set nomod" |
+
\ exe "set nomod" |
\ endif
+
\ endif
augroup END
+
augroup END
endif
+
endif
   
command Hexmode call ToggleHex()
+
command Hexmode call ToggleHex()
  +
 
function ToggleHex()
+
function ToggleHex()
" hex mode should be considered a read-only operation
+
" hex mode should be considered a read-only operation
" save values for modified and read-only for restoration later,
+
" save values for modified and read-only for restoration later,
" and clear the read-only flag for now
+
" and clear the read-only flag for now
let l:modified=&mod
+
let l:modified=&mod
let l:oldreadonly=&readonly
+
let l:oldreadonly=&readonly
let &readonly=0
+
let &readonly=0
if !exists("b:editHex") || !b:editHex
+
if !exists("b:editHex") || !b:editHex
" save old options
+
" save old options
let b:oldft=&ft
+
let b:oldft=&ft
let b:oldbin=&bin
+
let b:oldbin=&bin
" set new options
+
" set new options
setlocal binary " make sure it overrides any textwidth, etc.
+
setlocal binary " make sure it overrides any textwidth, etc.
let &ft="xxd"
+
let &ft="xxd"
" set status
+
" set status
let b:editHex=1
+
let b:editHex=1
" switch to hex editor
+
" switch to hex editor
%!xxd
+
%!xxd
else
+
else
" restore old options
+
" restore old options
let &ft=b:oldft
+
let &ft=b:oldft
if !b:oldbin
+
if !b:oldbin
setlocal nobinary
+
setlocal nobinary
 
endif
 
" set status
  +
let b:editHex=0
 
" return to normal editing
 
%!xxd -r
 
endif
 
endif
 
" restore values for modified and read only state
" set status
 
let b:editHex=0
+
let &mod=l:modified
 
let &readonly=l:oldreadonly
" return to normal editing
 
 
endfunction
%!xxd -r
 
endif
 
" restore values for modified and read only state
 
let &mod=l:modified
 
let &readonly=l:oldreadonly
 
endfunction
 
   
 
This will make vim automatically use xxd-format hex editing when the file is opened in binary mode, and will automatically (locally) set binary mode for .bin and .hex files! In addition, it provides a command "Hexmode" for entering the xxd format properly.
 
This will make vim automatically use xxd-format hex editing when the file is opened in binary mode, and will automatically (locally) set binary mode for .bin and .hex files! In addition, it provides a command "Hexmode" for entering the xxd format properly.

Revision as of 20:25, 3 October 2007

The help files for vim include the following advice for automating the xxd-style hex editing capabilities for vim:

 " vim -b : edit binary using xxd-format!
 augroup Binary
   au!
   au BufReadPre  *.bin let &bin=1
   au BufReadPost *.bin if &bin | %!xxd
   au BufReadPost *.bin set ft=xxd | endif
   au BufWritePre *.bin if &bin | %!xxd -r
   au BufWritePre *.bin endif
   au BufWritePost *.bin if &bin | %!xxd
   au BufWritePost *.bin set nomod | endif
 augroup END

There are a few problems with this approach: 1) Only files with a .bin extension are opened this way, even if editing a file in binary mode (e.g. with ++bin) 2) This sets the binary option for any future documents opened as well. If you use tabe, for example, or if you have Vim set up to open new files in tabs using --remote-tab-silent, then any new files opened will open in binary mode. 3) If you decide to edit a binary file without the xxd, the BufWrite autocommands will try to convert it with xxd -r anyway.

I've been able to fix all these problems as follows:

Put the following in your vimrc file:

 if has("autocmd")
   " vim -b : edit binary using xxd-format!
   augroup Binary
     au!
     au BufReadPre *.bin,*.hex setlocal binary
     au BufReadPost * 
           \ if &binary | exe "Hexmode" | endif
     au BufWritePre *
           \ if exists("b:editHex") && b:editHex && &binary |
           \  exe "%!xxd -r" |
           \ endif
     au BufWritePost *
           \ if exists("b:editHex") && b:editHex && &binary |
           \  exe "%!xxd" |
           \  exe "set nomod" |
           \ endif
   augroup END
 endif
 command 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
   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
 endfunction

This will make vim automatically use xxd-format hex editing when the file is opened in binary mode, and will automatically (locally) set binary mode for .bin and .hex files! In addition, it provides a command "Hexmode" for entering the xxd format properly.

In addition, I use the sendto menu in Windows XP for editing files in tabs with gVim. Here's some handy shortcuts to add for sending stuff to the hex editor set up above: 1) Launch in tabs in a new dedicated vim hex editor: "C:\Program Files\Vim\vim71\gvim.exe" -p -b -c "set binary" --servername HEXVIM 2) Launch in tabs in an existing dedicated vim hex editor: "C:\Program Files\Vim\vim71\gvim.exe" -b -c "set binary" --servername HEXVIM --remote-tab-silent