How can I use Vim as a hex editor?
You can use the
xxdcommand to transform a file in Vim to hex representation, doing
:enters command-line mode,
%matches whole file as a range,
!filters that range through an external command,
xxdis that external shell command
Giving an output like this, this is split into octet count/line (octets per line may be changed with parameter
xxdcommand), hex representation, and text representation:
0000000: 5468 6973 2069 7320 6120 7465 7374 0a41 This is a test.A 0000010: 6e6f 7468 6572 206c 696e 650a 416e 6420 nother line.And 0000020: 7965 7420 616e 6f74 6865 720a yet another.
Once you make the changes (in the hex part), you can go back to text with
-rcommand on xxd, so
This is a little rudimentary, though I like it in its simplicity and to keep in mind
xxd, which is an interesting command line tool (I don't have to do a lot of work on hex, though). You can use some recipes to handle this transformation in a more automatic way like the one described in Improved hex editing.
And rembember you can also use syntax highlighting for hex editing in vim with that command:
It's an independent program that is run from the command line, and it's installed independently from vim (I've seen it in several Linux distributions that didn't install vim by default). That's a UNIX command tool for me :-P
I think it worths mentioning that after you make the changes you **must** go back to the normal mode before saving the changes.
If one has a particular extension that one uses for binary files (such as exe, bin, etc), you may find it helpful to automate the process with the following bit of autocmds for your <.vimrc>. Change that "*.bin" to whatever comma-separated list of extension(s) you find yourself wanting to edit: " 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
You may want to look at: How do I navigate to topics in Vim's documentation?