5

As a developer who grew up on hardware with no MMU I have a habit of saving my work often, usually after every insert. Thus, when presented with the recovery dialogue after a crash or disconnect there is a very high probability that there were no unsaved changes and that the swapfile is identical to the saved file. I would like to reduce the friction between me and vim when trying to open files in this case.

Here is a recent example from the message shown to me when I edit a file after an unclean shutdown:

E325: ATTENTION
Found a swap file by the name "~/.vim/swapfiles/algorithm_params.h.swp"
          owned by: ana   dated: Thu Jan 11 16:09:00 2018
         file name: ~ana/src/algorithm_params.h
          modified: no
         user name: ana   host name: anavirt
        process ID: 8932
While opening file "src/algorithm_params.h"
             dated: Thu Jan 11 16:08:55 2018

(1) Another program may be editing the same file.  If this is the case,
    be careful not to end up with two different instances of the same
    file when making changes.  Quit, or continue with caution.
(2) An edit session for this file crashed.
    If this is the case, use ":recover" or "vim -r src/algorithm_params.h"
    to recover the changes (see ":help recovery").
    If you did this already, delete the swap file "/home/ana/.vim/swapfiles/algorithm_params.h.swp"
    to avoid this message.

Swap file "~/.vim/swapfiles/algorithm_params.h.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:

Ideally, I don't even want to see this dialogue. From what I understand, vim has the information necessary, but I may be wrong on these:

  • modified: no means that it knows that the swap file is identical to the saved file.
  • process ID: 8932 means that the process used to generate this file is no longer running, otherwise it would say (still running).

Is there any way to tell vim to just delete the swapfile and edit the main file without even asking, if these two conditions are met, and otherwise pop up this dialogue as usual?

  • 1
    Doubt there's a native solution but this plugin addresses some pain points: github.com/gioele/vim-autoswap – B Layer Jan 12 '18 at 10:49
  • 1
    Also, if you can't find a plugin that does exactly what you want the code in $VIMRUNTIME/pack/dist/opt/editexisting/plugin/editexisting.vim will give you a leg up in scripting a custom solution. (Might be in $VIMRUNTIME/macros in older versions of vim.) – B Layer Jan 12 '18 at 11:47
  • 3
    If you save after every insert (which is also what I do) maybe you simply don't want any swap file or backup file? I have this in my vimrc set noswapfile nobackup which I find pretty useful see :h 'swapfile' and :h 'backup' – statox Jan 12 '18 at 13:15
  • 3
    You can use my plugin Recover.vim together with :let g:RecoverPlugin_Delete_Unmodified_Swapfile = 1 – Christian Brabandt Jan 12 '18 at 19:04
1

Since you do not need the swap-functionality, you can disable it using :set noswapfile or include set noswapfile in your .vimrc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.