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?
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.
Yes, Vim loads the whole file into memory.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With