# Force prompt for whether to delete a swap file

How do you configure vim to prompt for deletion of swap files even if it believes another instance of vim may still be using the file? I still want to see the warning message ... just also have the option to delete the file and overrule vim.

The option below, for instance, doesn't have the (D)elete option. (where program.cc may potentially be modified by another vim instance)

(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 program.cc"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file "program.swp"
to avoid this message.

Swap file "program.cc.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort:
• Hmm, I'm not sure when vim does or doesn't allow the deletion option, but it shows for me. Is your file in a restricted area? What version of vim are you on? What OS are you on? – James Sep 8 '17 at 20:42

The "Delete it" option isn't displayed if the Vim process is still running; I can't find this documented anywhere but I looked it up in the source code; from memline.c, line 4512 (slightly simplified):

do_dialog(
[..]
process_still_running
? (char_u *)_("&Open Read-Only\n&Edit anyway\n&Recover\n&Quit\n&Abort") :
(char_u *)_("&Open Read-Only\n&Edit anyway\n&Recover\n&Delete it\n&Quit\n&Abort"), [..]);

The swap file embeds the process ID which created it, and if a process with that PID still exists it considers the process to be "running".

The swap message should display this information:

E325: ATTENTION
Found a swap file by the name "~/.vim/tmp/swap/swapy.swp"
owned by: martin   dated: Fri Sep  8 22:13:35 2017
file name: ~martin/swapy
modified: no
user name: martin   host name: arch.arp242.net
process ID: 17355 (still running)
While opening file "swapy"
dated: Fri Sep  8 22:13:35 2017

Note the process ID: 17355 (still running) line.

The most likely scenario is that you have another Vim instance running somewhere :-) You could kill it if you can't find it.

There is a small chance that the PID got re-used by another process though, in which case your only option is to quit Vim, manually remove the swap file, and restart it again:

$rm ~/.vim/tmp/swap/swapy.swp • hmm, makes sense. Is there a way to say "remove all the swap files corresponding to this buffer"? That way I can choose edit anyway, then remove the files. – Gregory Nisbet Sep 9 '17 at 1:33 • @GregoryNisbet AFAIK there isn't an easy way for this; I'm also not sure if that's what you really want, since it may cause problems in the Vim instance that already has this file open. – Martin Tournoij Sep 9 '17 at 2:21 • This is a particularly important answer if you have TMUX sessions in the background (e.g., your graphical desktop crashed and you logged back in - speaking for a friend :) ) - tmux list-sessions followed by tmux attach -t$NUM is your friend... tmux is simple and awesome. – zenaan Oct 1 '19 at 0:44

Something like this may work: first, based on this answer, you could create a separate location for all your swap files like this set directory^=\$HOME/.vim/swap//

Then, using the answer from here, place this

noremap <LEADER>s <C-w>o:sav! ~/.vim/.recovered<CR>:vs<CR><C-w>w:bn<CR>
noremap <LEADER>t  :wa<CR>:bp\|bd #<CR><C-o>

into your vimrc, the way that I get to mine is by using the terminal I type vim .vimrc which opens it and allows me to edit it.

Then follow this set of instructions:

Upon reaching the ATTENTION: Found a swap file... prompt, I

type r to recover the swap
type <LEADER>s to save the swapped version as .recovered and open the original in a new split
type e at the swap prompt to edit the original file
type :windo diffthis to diff the two files
make any necessary changes to the original
type <LEADER>t to write the original file, close it, and reopen it
type d at the swap prompt to delete the swap file
close the .recovered file (typing something like :bp<CR>:bp\|bd #<CR>).

The reason that I placed the first step is because it seems as though it would be easier to delete all your .swp and .swo if they are in the same location. Hopefully this helps.