Inspecting binary data in neovim
Viewing binary data in Neovim is not a new thing. There are already a few plugins, and a lot of content on how to do this. That's still not stopping us building another one. You know, just for the fun of it.
After having to inspect some binary data, I found myself spending quite a lot of time on online hex editors. Trying to get raw binary data into them became quite a pain, so the hunt for a solution started.
There is quite a lot of information about using the xxd command. It has all
the features I was looking for, converting to and from binary and hex, and
viewing the hex values of the data.
There were two main features I was looking for to optimize in my workflow. The
first is to automatically convert a binary .bin file to hex using xxd. The
second was to format a long hex string into a hex dump format.
Converting the bin file to a hex dump is quite simple with the :%!xxd trick.
You even get nice syntax highlighting out of the box with :set ft=xxd.
The next part I wanted to solve was formatting a hex string into a binary dump. Quite often when working with binary data you can log the hex data you receive from a network stream or something similar. Being able to format this into the same hex dump is valuable for seeing what you are actually receiving over the network.
XXD makes this super simple as well. You can use the -r flag to do the reverse
with XXD, then pass it back into XXD to make the binary dump.
echo "48656c6c6f20576f726c64" | xxd -r -p | xxdPackaging this up into a nice Neovim plugin was super simple and makes it really easy to inspect binary data. After adding some virtual text to display the hex, bin, and decimal value on the actual byte char, it became really nice to inspecting and decode network streams.