I like Vim's swap file system and I want to be prepared for the case I forget to :w, reusing this system. I think it's possible if I can change when Vim deletes the swap file, but I could not find the way firstly to stop Vim from deleting it. Do you know how to do it? or any alternative?
A few thoughts
As mentioned already, Vim does not quit without writing if one does not explicitly tell Vim not to. If one say
:q and there are any open, regular file(s), that is not saved it prompt you to either save or use
Sometimes one have made changes to a file that one do not want to save. In that case, if Vim kept the swap file, next time you open that file you get asked if you should recover the file or not. That could quickly become an issue. Should I recover or not? Did I mean to save or not last time I edited this file? One are more likely to know if one want to keep changes or not when exiting Vim, then when ever one re-edit the file.
The swap-files are normally "kept" if Vim exits unexpectedly. That is: Vim crashes, your system crashes, etc.
Another task for the swap-files is for vim to know if you are editing the file in another session.
How to keep
You can keep the swap file by:
:preservedone on the buffer
By issuing the
:preserve command Vim writes all the text of the file to disk. I.e. a snapshot of how the file is now. If buffer has preserved-flag set and
cpo-& the swap is not deleted. Note that if you do:
- then do changes,
The swap-file does not contain the changes done after
:preserve unless the changes has triggered a swap-write (See section above
So doing something like (Not recommended):
set cpo +=& autocmd QuitPre * :bufdo :preserve
would always sync and keep swap-files.
I would really not recommend it though, but that's not my business so to speak ;)
What would you do if you wanted to abandon changes? + Your system would be littered with swap-files all over.
There are other files and options that could be of interest. Some might be:
backup(Also see the following topics like
Personally I have backup directory set to
~/.vim/.backup and keep backups after write. As the backup file is updated to last old version on each write it does not have that much use, but every fifth blue moon it saves me hah.
I keep big Vim-info files
Keeping undo for files even after you exit vim. If you edit the same file later the undo history is preserved.
set undofile set undodir =~/.vim/.undo
Note of course that all of these slows down Vim to some degree, but the tradeoff is good if one find it useful.
You might also find
:h 'confirm' useful. I do not use it, but some find it nice.