Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would vim create a new file every time you save a file?

Tags:

linux

vim

I have a file named test:

[test@mypc ~]$ ls -i

4982967 test

Then I use vim to change its content and enter :w to save it.

It now has a different inode:

[test@mypc ~]$ ls -i

4982968 test

That means it's a different file already, why would vim save it to another file as I use :w to save to the original one?

You see, echo to a file will not change the inode, which is expected:

[test@mypc ~]$ echo v >> test

[test@mypc ~]$ ls -i

4982968 test
like image 697
Shady Xu Avatar asked Sep 29 '14 06:09

Shady Xu


1 Answers

It is trying to protect you from disk and os problems. It writes out a complete copy of the file, and when it is satisfied this has finished properly, renames this file to the required filename. Hence, new inode number.

If there were a crash during the save process, the original file would remain untouched, possibly saving you from losing the file completely.

like image 75
Max Avatar answered Oct 27 '22 07:10

Max