Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 588
gcali Avatar asked Mar 27 '12 19:03

gcali


People also ask

How do I edit a xxd file?

xxd is a linux command. Once the file is open, press ESC and then type :%! xxd -b and then press ENTER . Press ESC and then i for "INSERT" mode which allows you to edit.

Can vim edit binary files?

When I saw Tim Pope's excellent vim-afterimage script, it reminded me that Vim is completely capable as a binary file editor. Opening a file with -b or running :set binary makes Vim more suitable for editing binary files: textwidth and wrapmargin are set to 0.

How do I edit a binary file?

To open the Binary Editor on an existing file, go to menu File > Open > File, select the file you want to edit, then select the drop arrow next to the Open button, and choose Open With > Binary Editor.


1 Answers

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.

like image 97
heijp06 Avatar answered Oct 03 '22 10:10

heijp06