Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Sometimes Vim crashes and leaves me a swapfile. That's awesome. Then I open the file I was editing and Vim asks me if I want to recover. I do, thanks. When it's done, Vim tells me,

You may want to delete the .swp file now.

Why, yes, I do. How do I do that? I figured it would just start using the old swapfile as a swapfile again and clean it up when I quit, but that's not true. It makes a new one, cleans that one up, and when I open the file again it prompts me again to recover from the first one.

Surely I'm missing something.

share|improve this question

10 Answers 10

up vote 36 down vote accepted

A slightly simpler way would be to do this:

From the terminal emulator:

vim filename

Then if you choose recover, and you want to delete the swap then you first write the file and then do this from within vim:

:e          // no argument needed. It assumes the current buffer.

...and choose delete this time. I just found out about this, and I think I'll do that from now on. Then I won't accidentally overwrite a working file either, if the recovery turned out corrupt (though I've never tried that before).

There are also relevant tips on this question on Stack Overflow. (where I found this tip)

share|improve this answer
Brilliant! Thanks! – Peeja Dec 13 '13 at 15:43
Thanks! Friday just got that little bit better :) – br3w5 May 15 '15 at 13:12
I don't a delete option, there is only: [O]pen (E)dit (R)ecover (Q)uit (A)bort, what should I do? – Arne Oct 31 '15 at 17:24
@Arne: From my experience that usually means the file is currently opened in another instance of vim? – miyalys Oct 31 '15 at 17:32

To Clean out ALL vim Swap files in a Dir:

If you are sure you don't need any vim swap files in a directory and want to get rid of them you can use the following command in the directory while vim is not running.

find ./ -type f -name "\.*sw[klmnop]" -delete

Some inspiration and thoughts came from:!topic/vim_use/JBHSs3kPPJU

'klmnop' may be overkill, but that usually ensures I get all of them.

share|improve this answer
If going that route how about just rm *.sw* ? It won't delete directories either (not recursive), though it will delete files called .sw and then something other than "klmnop". (Like swf's, though it hasn't been a problem for me.) But you could also modify the rm command like that: rm *.sw[klmnop] – miyalys Apr 17 '15 at 8:14
@miyalys not sure why but even with -r I can't get rm to work recursively to delete files in subdirectories. – Shwaydogg May 23 '15 at 18:34
Ah right, yeah I don't think my suggestion in my comment here would work recursively unlike yours. – miyalys Sep 19 '15 at 18:35

Type ls -a which lists ALL the files in the directory

type rm .whatever.your.swp is and press enter

Its that simple.

Any file that shows with . in front is a hidden file and is not normally seen.

Remember that changes are immediate and permanent so be careful.

share|improve this answer
Did you mean ls -a ? – Kevin Panko Dec 21 '13 at 17:45
That's not very simple, though. I have to leave Vim, navigate the swap directory, remember the name of the file, and possibly decide whether it's file.swp or file.swo. Then I have to get back to where I was. Hitting :e is a much simpler solution. – Peeja Dec 21 '13 at 19:41
ls: cannot access +A: No such file or directory – Jon Jun 17 '15 at 15:15

No, AFAIK you're missing nothing. Vim continues to keep the swapfile as a backup until you explicitly delete it.

Simply save and quit and reopen the same file again. You'll be prompted (again) with

[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort, (D)elete it

Now just press d :)

share|improve this answer
Really? I mean, I knew that. But that's stupid. It's certainly not "simple". – Peeja Sep 27 '12 at 15:12
I don't know any better way to do that and I can't even find any reference to a command to delete swapfiles. If nobody provide a better answer you can always setup a mapping by yourself... – Avio Sep 27 '12 at 15:23
By the way, what's the difference between quit and abort? – trusktr Oct 3 '13 at 5:10
hmm Vim never prompts me to delete the swap file – Gregory Pakosz Jun 20 '14 at 13:55
there is no Delete option in my case. what should I do? (I saved, quit, and reopened) – Paschalis Dec 4 '14 at 10:14

Vim, like most Unix commands, is simple and explicit, rather than assuming and implicit.

You recover with (r). It you don't like the recovery or the recovery is corrupt, you can discard it. If you do like it, you can save the file. The recovery file is not deleted for you because this is not explicit and will not be correct for 100% of situations.

So you (r)ecover, (w)rite and (q)uit, then either edit again and choose (d)elete or "rm .myfile.js.swp" and edit again. This is quick to do and is always, from Vim's point of view, 100% correct behaviour.

share|improve this answer
Okay, then is there a way to get vim to give me the path to the swap file? I'm happy to run a second command to delete the swapfile (in fact, I like the explicitness of that), but I'd like a command to delete the swapfile wherever it is. For instance, I've got a separate swapfile directory. If two open files have the same basename, I can't programmatically decide which is the correct swapfile for the current buffer. – Peeja Sep 27 '13 at 14:33
True, but providing a simple "Recover and delete" option would be nice. It's also probably easy tom implement. I'll do it someday when I'm done going to school if it hasn't been done already. – trusktr Oct 3 '13 at 5:09

From the manual :h swap:

You can see the name of the current swap file being used with the command: :sw[apname]

share|improve this answer

You can use,

rm -f .<filename>.<ext>.swp


rm -f .rc.local.swp
share|improve this answer
This technically works, but it's nowhere near as easy at the current accepted answer. – Peeja Mar 18 '15 at 14:36

Btw. some plugins do that automatically for you: autoswap.vim

from the description:

  1. Is file already open in another Vim session in some other window?
  2. If so, swap to the window where we are editing that file.
  3. Otherwise, if swapfile is older than file itself, just get rid of it.
  4. Otherwise, open file read-only so we can have a look at it and may save it.
share|improve this answer

if you're asked then press D to delete current file's swap file.

if you are not then you have to do it manually.

one alternative way to do this is:

to locate/search file:

find | grep ".searchRefineVertTabs.jsp.swp"

and then delete this file:

rm ./.searchRefineVertTabs.jsp.swp
share|improve this answer
I am asked, but my choice is to recover. The question is how to delete it just after I've recovered, since it's no longer necessary. It seems like a failure of vim not to do that for me. But the accepted answer is the cleanest workflow I've found so far. – Peeja Sep 24 '15 at 16:00

These files were annoying for me too, but i set option in .vimrc - set noswapfile to prevent vim create it and, instead, keeping files in memory.

share|improve this answer
According to the OP, swapfiles are useful, so turning them off might not be the best option. – Kusalananda Jun 24 at 12:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .