Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim, automatically remove swap file when file is saved

Tags:

vim

Is it possible to automatically remove the swapfile when the file is saved, and automatically create it again when the file is changed?

And is this a good idea?

EDIT: The reason for wanting to to this is so that I don't have to go through the diff process for files that had no changes after last save if the computer suddenly ran out of power or crashed.

The diff process referred to is; first open the file read only, saving it under a different filename, vimdiffing it and in the end delete it.

It doesn't happen often, but is just as annoying when it does.

like image 377
Orjanp Avatar asked Aug 25 '10 13:08

Orjanp


People also ask

Where are Vim swap files saved?

While editing a file, you can see which swap file is being used by entering :sw . The location of this file is set with directory option. The default value is .,~/tmp,/var/tmp,/tmp . This means Vim will try to save this file in the order of . , and then ~/tmp , and then /var/tmp , and finally /tmp .

What is SWP file in Vim?

An SWP file is a swap file created by the Vi text editor or one of its variants, such as Vim (Vi iMproved) and gVim. It stores the recovery version of a file being edited in the program. SWP files also serve as lock files, so no other Vi editing session can concurrently write to the currently-open file.

Can I delete SWP files?

If you are -certain- that the file on the disk is the correct one and you don't need the "autosaved" information in the swapfile, you can simply type "D" to delete the swapfile. The message will go away and you can continue working.


2 Answers

Is it possible to automatically remove the swapfile when the file is saved, and automatically create it again when the file is changed?

And is this a good idea?

Deletion and creation of a file are relatively slow operations. It makes sense to keep the swap file in case if one going to keep editing. Or why else one would keep the VIM running?

Additionally VIM uses the swap file to detect attempts to edit the same file from different VIM instances.

All in all, I think it is a bad idea to try to remove the file.

P.S. Worth noting that when VIM is started in read-only mode as view or vim -R, it doesn't create the swap file. But if once you change the file, the swap file is going to be created.

like image 159
Dummy00001 Avatar answered Oct 13 '22 03:10

Dummy00001


I am assuming you have a long running vim session like I do (my first window in my screen session is always vim and it usually lives for several days, almost for the entire duration of the uptime). I think the swapfile is not kept around, if you simply close the buffer, unless you have the 'hidden' setting. What happens is that when you have the 'hidden' turned on for buffers, vim tries to keep all the undo history of the buffer, and so has to have the swapfile accessible, in case you come back to the buffer and start undoing changes. As for me, I don't like this feature and keep it turned off (which is the default) and so only have to deal with the swapfiles for those buffers that I actively have loaded in a vim window at the time of crash. Since I have macros that autosave my session every 5min., and since I also disable the Vim's default swapfile detected prompt, restarting my session after a crash is fairly painless.

like image 43
haridsv Avatar answered Oct 13 '22 03:10

haridsv