Sometimes one needs to edit a binary file. The easiest way to do this is using a hex editor which displays all bytes in a file as hex.

The vi editor has a handy feature that does just this:

  1. open the file using the -b option. This turns off any automatic formatting. For example: vi -b data
  2. convert the binary data into hex format by entering :%!xxd
  3. do the required edits (move around using the cursor keys, start inserting characters with the i key, leave inserting with the ESC key, delete characters with the x key)
  4. convert the hex data back to binary by entering :%!xxd -r
  5. write back your data by entering :w followed by :q or leave without writing the data by entering :q!


Related articles