2

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?

5
  • I'm not sure if I follow when Vim deletes the swap file in your case; if you don't use :w – which you don't if I understand you correctly – then the swap file should stay around. Jun 16, 2022 at 14:01
  • Vim only keep swap files when Vim is closed unexpectedly. In my case when many files are on buffer along with some terminals(which are not closed by :q, at least on Win10) and I use :q!, then swap files are deleted.
    – Dugesia7
    Jun 16, 2022 at 15:55
  • Try :wall | quit! instead, or use backup files? :q! is not really considered "closing unexpectedly."
    – D. Ben Knoble
    Jun 16, 2022 at 17:46
  • Oh right; I assumed you closed the Vim window or something. The obvious question is: why are you using :q!? This explicitly tells Vim to "quit without saving". You should probably get out of the habit of using :q! Jun 17, 2022 at 8:08
  • Umm, there seems to be no way to stop deletion of swap file? Cron job and rsync seems to be useful, though it requires a bunch of scripting. Vim's backup file is for when the file hasn't been saved correctly or lost, and not much helpful in my case.
    – Dugesia7
    Jun 18, 2022 at 0:30

2 Answers 2

2

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:

  1. :preserve,
  2. then do changes,
  3. 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:

  1. Backup: :h 'backup', backup (Also see the following topics like backupcopy 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.

  1. Viminfo :h 'viminfo'

I keep big Vim-info files

  1. 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.

1

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.

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.