Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does vim save files?

I believe it's designed to be atomic, and I've heard that vim will save to a temporary directory and then mv the temp file over the old file to ensure that EITHER the old version OR the new version is present, and never half of one or no file at all, even if the power is turned off mid-save.

Does anyone have more details?

like image 478
nornagon Avatar asked Nov 26 '12 11:11

nornagon


People also ask

Where does Vim save files?

As said by others: by default it saves in the directory where you started it. But if you aren't aware in which directory you started, then a way to find out is to use the :pwd com in vim. This will output the current directory. That's where vim will store the file.

Does Vim load entire file into memory?

Yes, Vim loads the whole file into memory.

How do I save a file in Linux VI?

To save a file, you must first be in Command mode. Press Esc to enter Command mode, and then type :wq to write and quit the file. The other, quicker option is to use the keyboard shortcut ZZ to write and quit. To the non-vi initiated, write means save, and quit means exit vi.


1 Answers

How this is done depends on the values of several options.

The main option that affects this is backupcopy, on unix systems this defaults to yes on other systems it defaults to auto. Only if it is set to no can you be assured that writes will be done like in your question. When set to yes vim will instead overwrite the original file contents after making a backup copy. If set to auto, it will behave as in the question if it detects that attributes can be passed on and the file isn't a link (either a symlink or a hard link). There are additional values for the option to have it break links, see the help for that option.

Other options that affect this are backup, writebackup. At least one of those must be set for a backup to be made at all. If no backup is done, the original file contents will be overwritten. The writebackup option defaults to on as long as vim was compiled with support for it. There's also the backupskip option which specifies a list of file patterns for which no backups will be done; if one of those patterns match the file, again no backup will be done and the original file contents will be overwritten.

like image 200
qqx Avatar answered Oct 19 '22 12:10

qqx