Search code examples
vimmp3raw-dataxxd

Vim modifying a file if used with xxd


I'm trying to understand how ID3 tags work, so, after reading some documentation, I started to look at some mp3's raw data. Vim is usually my editor of choice, so, after some googling, I found out I could use xxd to see an hex representation of my files by invoking

:%!xxd  

Everything worked fine, but when I put everything back in order with

:%!xxd -r  

and quit, I found out that the file was modified; vlc could no longer play it, and diff told me the files differed. I thought I modified something by accident, but further experiments showed me that even opening the file and using xxd and then xxd -r changes somehow the file.

Why is that? How can I prevent it from happening? Am I doing something wrong?


Solution

  • Obviously if you do not intend to change anything to a file you could quit vim using :q!.

    As @RunHolt points out, vim and xxd can change a binary file. e.g. changing LF to CRLF or appending an LF character at the end of file.

    You can prevent this by setting the binary option:

    Either start vim as: vim -b filename or type :set binary before loading the file into the buffer.