开发者

Vim, automatically remove swap file when file is saved

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 s开发者_开发知识库o 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜