While editing binary files in Vim, one needs to be careful about setting the binary option before editing a binary file or opening the binary file with `-b` option.
For example,
printf "\x00\x01\x02\x03" > a.bin
vim a.bin
:%!xxd -g1
would display
0000000: 00 01 02 03 0a .....
(The `printf` and `vim` commands are entered in the shell. The `:%!xxd` command is entered in Vim.)
In the above example, a newline has been added by Vim where none existed in the binary file. In fact, if you then run `%!xxd -r` followed by `:w`, this additional newline would be written back to the file. Here is the right way to preserve the binary data in a binary file.
printf "\x00\x01\x02\x03" > a.bin
vim -b a.bin
:%!xxd -g1
would display
0000000: 00 01 02 03 ....
Only the four bytes in the file appear now. Alternatively, one may enter `:set bin` in Vim before editing a binary file.
Another useful one is `%!jq .` to pretty print a json file.