16

From :help swap-file:

Updating the swapfile

The swap file is updated after typing 200 characters or when you have not typed anything for four seconds. This only happens if the buffer was changed, not when you only moved around. The reason why it is not kept up to date all the time is that this would slow down normal work too much. You can change the 200 character count with the 'updatecount' option. You can set the time with the 'updatetime' option. The time is given in milliseconds. After writing to the swap file Vim syncs the file to disk. This takes some time, especially on busy Unix systems. If you don't want this you can set the 'swapsync' option to an empty string. The risk of losing work becomes bigger though. On some non-Unix systems (MS-DOS, Amiga) the swap file won't be written at all.

From this I have a few questions:

  1. Does this mean Vim autosaves your work from time to time?

  2. "After writing to the swap file Vim syncs the file to disk." What does this mean? Is this refering to the file being written first to memory and then to the disk or is it something else?

  3. Say I'm taking class notes: I write fast and there's little need for me to leave Insert mode. But many times I change to Normal just to save my work. Do I need to do this or does Vim make sure that most of my work is saved even if I don't leave Insert mode?

2 Answers 2

11
  1. Yes it saves to swap file.

You can do a quick test to get a feel of this. Go to a directory. Say vim notest.txt and write some text. You do not even have to save the file.

If you list files in the working directory you will find a swap file. Copy it to a different directory (where you have no "notes.txt" file, as say vim notest.txt.

You get a note about swap existing, it being modified, and the options of open as read only, recover etc. Choose recover and voila! A "copy" of your file.

  1. Yes, swap is held in memory.

When Vim reads a file it adds it to RAM. That is both for existing and new (still unsaved files). As swap is saved it is flushed to disk. The HDD. From RAM.

  1. Yes and no.

With default settings most of your work are saved. That is for every 200 characters entered, or when yo have a 4 second pause. Though I trust the swap file to be saved, I like to save my work when I know I'm on an unstable system. But, that said, one are fairly certain to keep the work unless one should be very unlucky and the hosting system going panic just after writing a very important sentence.

In short it is enough to save when the lecture is over. If the system should halt for what ever reason in midst of a class, the work is till there in the swap.

Of course: If you are very unlucky a crash can occur whilst swap file is being written. As a result swap file could be corrupted. Have never experienced this, and not sure which parts of the file might be messed up, but might worth a thought. As the swap file is binary (in lack of a better word), it seems to reason it is more fragile then a plain text file.


As a side-note. At least when using working directory for swap, the swap file is location agnostic when it comes to recovery. As such you can copy the swap file to a new location and recover the file as a new file. Only note that the file-name is hard coded into the swap. As such you can not say:

cp .foo.txt.swp .bar.txt.swp

and

vim bar.txt

and expect the swap file of foo.txt.

If you are very interested you can do (on *nix):

xxd -s 108 -l 800 -ps .foo.txt.swp | xxd -r -p

to get the path (with some quirks) of original file. As you'll see the full path is added, e.g.:

~user/blah/blah/notes.txt

but only the basename is used on swap check.

In that regard you can also check out the vim -r option - which lists swap files in current and lower directories. See :h crash-recovery with sub-topics.

0

For me is fine to save the buffer whenever text is changed:

autocmd TextChanged,TextChangedI <buffer> silent write

I found it here. It works for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.