Edit binary file with vim using the xxd command

Edit binary file with vim using the xxd command

To check binary file content xxd <file name>

To edit a binary file

  • Open the binary file normally with vim vim <file name>
  • Convert them to xxd human-readable format :%!xxd
  • Edit the hex part in the left
  • Convert xxd human-readable format back to binary :%!xxd -r
  • Save modified content :w

Here are some useful vim commands to work with binary file

  • :as(scii) or ga: print ASCII value of the character under the cursor
  • g8: print the utf-8 encoded binary of the character being displayed under the cursor, regardless of the value of fenc
  • 8g8: find the first invalid utf8 byte sequence.
  • To prevent vim from appending a newline character a0 at the end of file :set binary, then :set noeol.
  • To open a file in binary format, without de-coding its content. In other words, utf-8 compatible characters are shown as human-readable while illegal characters are shown as hex value

:set binary then :e (you must re-open the buffer)

or

vim -b <file name>

or

:e ++binary


Sematic binary format read/write.

fq is a handy binary inspector/decoder written in Go. Although it is still in early development, it is doing a very great job. Check it here: https://github.com/wader/fq.

screenshot of fq
Buy Me A Coffee