Monday, February 22, 2010

Re: Hex edit in vim

Hi felix!

On Mo, 22 Feb 2010, felix hoo wrote:

> When I edit the binary file in hex mode in vim. I would like to have the
> ascii view updated once I change the hex value. Is it possible to do so?
>
> Current behavior is :
>
> 0000000: 3132 372e 302e 302e 3109 0920 2020 206c 127.0.0.1.. l
> 0000010: 6f63 616c 686f 7374 0a ocalhost.
>
> Change the first byte. But the ASCII view does not changed accordingly.
>
> 0000000: 3232 372e 302e 302e 3109 0920 2020 206c 127.0.0.1.. l
> 0000010: 6f63 616c 686f 7374 0a ocalhost.
>
> I would like to vim act like this
>
> 0000000: 3232 372e 302e 302e 3109 0920 2020 206c 227.0.0.1.. l
> 0000010: 6f63 616c 686f 7374 0a ocalhost.

I think, the best solution would be, to filter your file back through
xxd -r and return to the hex view by filtering it again through xxd.

Nevertheless, you could update the view manually, something like this
could work:

fun! <sid>HexVal(val)
let val=substitute(a:val, '\s\+', '','g')
let list=split(val, '..\zs')
call map(list, 'nr2char(str2nr(v:val,16))')
call map(list, 'empty(v:val)?".":v:val')
let val=join(list, '')
let val=substitute(val, '[^[:print:]]', '.', 'g')
return val
endfun

fun! <sid>UpdateHexDisplay() range
s/\%51c.*$//e
s/: \zs\(\%(\x\{4\}\s\)\{,8}\).*$/\=submatch(0).repeat(' ',
\41-len(submatch(0))).<sid>HexVal(submatch(1))/
endfu

com! -range HexUp :call <sid>UpdateHexDisplay()


And then run :HexUp on your changed lines. This is however only barely
tested and might corrupt your file. You should be very careful, when
using this.

regards,
Christian

--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

No comments: