Vim modifying file if used with xxd

I am trying to understand how ID3 tags work, so after reading some documentation I started looking at some raw mp3 data. Vim is usually my editor of choice, so after some googling I found that I could use xxd to view the hex representation of my files by calling



Everything worked fine, but when I put everything ok with

:%!xxd -r  


and exit, I found out that the file was changed; vlc couldn't play anymore and diff told me the files were different. I thought I changed something by accident, but further experiments showed me that even opening the file and using xxd and then xxd -r modifies the file in some way.

Why? How can I prevent this? Am I doing something wrong?


source to share

3 answers

Obviously, if you're not going to change anything to the file, you can exit vim

with :q!


As @RunHolt points out, vim

and xxd

can modify the binary. for example changing LF to CRLF or adding an LF character at the end of the file.

You can prevent this by setting the option binary


Either run vim

as: vim -b filename

or type :set binary

before loading the file into the buffer.



You probably didn't download the file as a binary with vim -b

. That is, the damage has already been done.


- red herring; xxd

It followed by xxd -r

, transparently. It is designed for editing binary files. xxd

does not add bytes; it produces an exact hexdump that changes to exactly xxd -r

(if you don't damage it).

For viewing only, you can simply run xxd

from the shell:

$ xxd binaryfile | vim -     # just use vim as a reader 


I edited executables from vim -b

and filtered through xxd

and back through xxd -r

. They did a great job.

In addition, xxd is a Vim-specific program that comes with the Vim distribution. It may be helpful od

for you to know for example

od -tx1 file




In Windows binaries (not sure about other platforms) :%!xxd

puts an end-of-file marker in the last two bytes (0x0d, 0x0a). %!xxd -r

Doesn't delete them for some reason .

I often delete them manually (just remove both characters than the mileage %!xxd -r


Perhaps this can be fixed with xxd




All Articles