Vim developers and users have been debating on methods to deal with the swap file and multiple Vim processes for a long time. Some plugins did come out of it.
The plugin that I use for that is vim-autoswap, but I do make changes to its code. When you do :w
Vim first updates the swap file and then the file, e.g.
$ echo yay > dummy
$ vim dummy
:w
:!ls -a --fulltime *dummy
-rw-r--r-- 1 grochmal users 4 2017-02-26 21:19:52.995699920 +0000 dummy
-rw-r--r-- 1 grochmal users 12288 2017-02-26 21:19:47.819084930 +0000 .dummy.swp
Therefore the swap file is older than the file you're editing when you perform the write (:w
). And the vim-autoswap plugin will delete a swap file older than the file itself.
On the other hand, the plugin would not be able to figure out that the swap file is older in some cases. For example, in a second test where I perform the :w
quickly after opening the file I get the following:
-rw-r--r-- 1 grochmal users 4 2017-02-26 21:22:14.144290852 +0000 dummy
-rw-r--r-- 1 grochmal users 12288 2017-02-26 21:22:14.104291252 +0000 .dummy.swp
The swap file is older alright, by 0.4 seconds. But Vim's getftime()
can only deal with seconds, so vim-autoswap will fail to figure out that the swap is older.
What I do to deal with that case is that I change line 61 in the plugin from:
elseif getftime(v:swapname) < getftime(a:filename)
to:
elseif getftime(v:swapname) <= getftime(a:filename)
And then I do:
$ echo yay > dummy
$ vim dummy
:w
<Ctrl+Z>
$ vim dummy
Vim (with the plugin) gives me a warning message saying:
Old swapfile detected... and deleted
But if it takes me more than one second making changes to the file before giving the SIGSTOP (Ctrl+Z
) then the plugin discovers the that the swap was updates and tells me:
Swapfile detected, opening read-only
So I know that it is very likely that I have unwritten changes in the other process.
:set noswf
?