Edit binary files in Vi

Technology

cafebabe_20080809.jpg

If you’ve ever wanted to examine or edit a binary file in your favorite text editor, there’s an easy way to simulate a vi hex mode. To do this, you just filter the file’s contents through the xxd hex dump utility, a trick that can be accomplished right within the vi/vim interface.

To convert a file to hex dump representation, just load your file in vi and type the following:

:%!xxd

This sends the entire contents of the opened document to xxd and loads in the result. At this point, you can view or edit any of the hex data. The ASCII representation is listed to the right, though editing this region will not affect the hex portion of the file.

When you are done, you’ll want to convert things back into their binary format before saving. To do this, you run things through xxd again, but this time with the -r option:

:%!xxd -r

Your file should be returned to illegible gibberish, which you can save back out with :wq.

A funny thing I just noticed: OS X binaries all start with the same 4 bytes which, in hex, spell out the phrase “cafe babe”. This is just a magic number used to identify the file as an OS X binary, but it’s hard not to ascribe some deeper meaning. ;)

8 thoughts on “Edit binary files in Vi

  1. Anonymous says:

    what will solution in ajax image editor in php on cofig.inc.php file what will path of  $imagemagick_dir in root

  2. Anonymous says:

    what will solution of aie in phpi have a problem with config.inc.php file what will path of all the variable n config file in localhost .please tellme fast

  3. Tom says:

    nice, there are a lot of things you can do with these types of scripts, thanks

Comments are closed.

Discuss this article with the rest of the community on our Discord server!
Tagged

ADVERTISEMENT

Maker Faire Bay Area 2023 - Mare Island, CA

Escape to an island of imagination + innovation as Maker Faire Bay Area returns for its 15th iteration!

Buy Tickets today! SAVE 15% and lock-in your preferred date(s).

FEEDBACK