Wednesday, October 5, 2022

Editing Binary Files on Vim Editor

 Here's a quick method I use to edit binary files on the vim editor. Suppose that the binary file name that we need to edit is called hello. Let's open it using the xxd hex viewer utility and pipe it to the vim editor as follows using the terminal. This produces the hex view of the binary file on the vim editor.

xxd hello | vim -

Now, we are good to go with editing the hex values in the file as you would do with any text file on the vim editor. Once we are done with the editing, we can now convert this updated hexdump view on the vim editor back to a new binary file. In order to do that, go to the command mode of the vim editor, and type and enter the following command. It will save the edited binary into a new file; the new file can be named anything while I have made it to be hello-new.

%!xxd -r > hello-new

Now, you can safely quit the vim editor with q! on the command mode as you would always quit it. :) Cheers!

References:

[1] https://vi.stackexchange.com/questions/343/how-to-edit-binary-files-with-vim