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?
2 Answers
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 :q!
/ :qa!
.
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:
cpoptions
having&
:preserve
done 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:
:preserve
,- then do changes,
- then
:q!
The swap-file does not contain the changes done after :preserve
unless the changes has triggered a swap-write (See section above :h :preserve
).
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.
Other files
There are other files and options that could be of interest. Some might be:
- Backup:
:h 'backup'
,backup
(Also see the following topics likebackupcopy
etc.)'writebackup'
,patchmode
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.
- Viminfo
:h 'viminfo'
I keep big Vim-info files
- Undo-file:
:h undo-redo
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.
Set up a cron job to rsync the swap files to another location.
That being said, I have no idea what you are asking. Vim warns you if you quit without writing first.
:w
– which you don't if I understand you correctly – then the swap file should stay around.:wall | quit!
instead, or use backup files?:q!
is not really considered "closing unexpectedly.":q!
? This explicitly tells Vim to "quit without saving". You should probably get out of the habit of using:q!