What's the easiest way to delete Vim swapfiles I've already recovered from?

89

31

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.

Peeja

Posted 2012-09-27T14:47:47.173

Reputation: 2 079

See here https://vi.stackexchange.com/questions/13518/force-prompt-for-whether-to-delete-a-swap-file?newreg=20989544783642b4b37f610b9b0656cf "The "Delete it" option isn't displayed if the Vim process is still running" and note the comment re TMUX sessions i.e. be sure you don't have Vim still running (also, GNU screen) in the background somewhere, see here https://vi.stackexchange.com/questions/13518/force-prompt-for-whether-to-delete-a-swap-file?newreg=20989544783642b4b37f610b9b0656cf#comment38464_13524

– zenaan – 2019-10-01T00:50:38.343

Answers

86

A slightly simpler way:

From the terminal emulator:

vim filename

Then choose recover, and if you want to delete the swap you write (save) 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).

miyalys

Posted 2012-09-27T14:47:47.173

Reputation: 1 757

17I don't a delete option, there is only: [O]pen (E)dit (R)ecover (Q)uit (A)bort, what should I do? – Arne – 2015-10-31T17:24:37.323

4@Arne: From my experience that usually means the file is currently opened in another instance of vim? – miyalys – 2015-10-31T17:32:30.743

1The [D]elete option only appeared after I updated to Vim 7.4 – jmlarson – 2017-03-30T15:09:34.530

@Arne I had the same issue. But after some time, a hour or so, I restarted vim and the option was available. PC wasn't restarted in that time. vim may think that another vim process is running. That's why it doesn't show the option. – ka3ak – 2017-04-24T11:02:13.137

8For those who say they don't see a (D)elete option, see :help swap-exists; quote On Unix this choice is only offered when the process that created the swap file does not appear to be running. I.e. perhaps you have another vim process running which is causing this option to be hidden. It might also explain why you have a swp file in the first place ! – Lqueryvg – 2018-04-01T12:34:19.627

40

To Clean out ALL vim Swap Files in a Directory:

If you are sure you don’t need any vim swap files in a directory tree and want to get rid of them, you can use the following command in the directory while vim is not running (not even in another window or even a different login session):

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

Some inspiration and thoughts came from Removing junk .swp files at Google Groups.  This will delete all files whose names end with .swk, .swl, .swm, .swn, .swo, or .swp in the current directory tree.

This might not be what you want, for several reasons:

  • As stated, it searches the current directory tree ; i.e., the current directory and all subdirectories, recursively.  That goes beyond what the question asks for, and may be considered to be overkill.
  • As stated, it will delete all files whose names end with .swk, .swl, .swm, .swn, .swo, or .swp, and not just .swpGary Johnson says,

    Also, not all swap files end in .swp.  If Vim needs to create a swap file and one ending in .swp already exists, Vim will use the extension .swo for the new one and .swn after that.  I think it just continues backwards through the alphabet.  So using something like
      \*.sw[nop] 
    

    or even

      \*.sw? 
    

    would be more thorough.

    The original author of this post says,

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

  • On the other hand, it might be underkill.  rouble suggests

    find . -type f \( -name ".*.s[a-v][a-z]" -o -name ".*.sw[a-p]" \) -delete
    

    which matches all (lower-case) three-letter extensions ranging from .saa to .swp.  He adds, “Be careful though, while this is a more complete solution, it will take out any .svg image files and .swf adobe flash files.  You may want to modify the regex if you work with those files.”  It also matches .sav, .snd (sound data), .sql, .src, .srt (SubRip video subtitle format), and many other common extensions.

  • It might be overkill.  As stated, it will delete all files whose names end with .swk, .swl, .swm, .swn, .swo, or .swp.  But, apparently, vim swap files are commonly “dot” files.  If you edit hello.cpp, the swap file might be .hello.cpp.swp.  As shown (but not explained) in rouble’s answer, it is safer to delete only files whose names begin with a dot; e.g., with

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

Shwaydogg

Posted 2012-09-27T14:47:47.173

Reputation: 519

14

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.

Wolfy

Posted 2012-09-27T14:47:47.173

Reputation: 149

1ls: cannot access +A: No such file or directory – Jonathan – 2015-06-17T15:15:51.263

this is what i need – user5510975 – 2018-11-03T07:12:57.553

4That'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 – 2013-12-21T19:41:01.197

7

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.

toogley

Posted 2012-09-27T14:47:47.173

Reputation: 205

6Can anyone tell me why vim cannot have sensible behavior by default, without plugins? – Pavel Šimerda – 2017-08-13T06:46:57.387

6

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 :)

Avio

Posted 2012-09-27T14:47:47.173

Reputation: 692

5there is no Delete option in my case. what should I do? (I saved, quit, and reopened) – Paschalis – 2014-12-04T10:14:05.050

You don't need to quit vim first, just type :e<return> once you have chosen "edit anyway" or "recover" on the file, and you will get the option to delete it – 00prometheus – 2017-07-17T23:30:45.823

11Really? I mean, I knew that. But that's stupid. It's certainly not "simple". – Peeja – 2012-09-27T15:12:28.380

1By the way, what's the difference between quit and abort? – trusktr – 2013-10-03T05:10:28.043

2

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.

Oli Comber

Posted 2012-09-27T14:47:47.173

Reputation: 21

1Most unix commands are simple, explicit and have sensible default behavior. Vim recovery is not simple, the UX is as bad as in LibreOffice, the behavior is plain wrong and the result, in many cases, is data loss. – Pavel Šimerda – 2017-08-13T06:49:00.617

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 – 2013-09-27T14:33:33.567

1True, 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 – 2013-10-03T05:09:46.430

1

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.

Dmitrii

Posted 2012-09-27T14:47:47.173

Reputation: 185

According to the OP, swapfiles are useful, so turning them off might not be the best option. – Kusalananda – 2016-06-24T12:14:58.737

Don't turn off swap files. If your machine crashes, you'll lose your work. – Lqueryvg – 2018-04-01T11:47:33.557

Of course this option implies risks, and as i know these swap files were made at the emerging of vi* editiors when it's crashes happened very often. Now i could say vim works stable and they are exists as legacy thing, imho. – Dmitrii – 2018-04-01T19:18:40.813

1

Here is a more complete regex to clean all vim swap files based on Github's own gitignore file for Vim

$ find ./ -type f \( -name "\.*\.s[a-v][a-z]" -o -name "\.*\.sw[a-p]" \) -delete

Be careful though, while this is a more complete solution, it will take out any .svg image files and .swf adobe flash files. You may want to modify the regex if you work with those files.

rouble

Posted 2012-09-27T14:47:47.173

Reputation: 554

1

Here is a snippet to add in .vimrc. It deletes all the swap files that are associated to the current file buffer and reset swap extension.

function! DeleteFileSwaps()
    write
    let l:output = ''
    redir => l:output 
    silent exec ':sw' 
    redir END 
    let l:current_swap_file = substitute(l:output, '\n', '', '')
    let l:base = substitute(l:current_swap_file, '\v\.\w+$', '', '')
    let l:swap_files = split(glob(l:base.'\.s*'))
    " delete all except the current swap file
    for l:swap_file in l:swap_files
        if !empty(glob(l:swap_file)) && l:swap_file != l:current_swap_file 
            call delete(l:swap_file)
            echo "swap file removed: ".l:swap_file
        endif
    endfor
    " Reset swap file extension to `.swp`.
    set swf! | set swf!
    echo "Reset swap file extension for file: ".expand('%')
endfunction
command! DeleteFileSwaps :call DeleteFileSwaps()

Once encounter with the predicament, one can execute :DeleteFileSwaps This is great if combine with :windo or :tabdo commands.

:tabdo DeleteFileSwaps 

Further details: A file can have more than 1 swap file. The reason because the swap file, with extension of .swp, still exist and vim will keep creating new ones because of it. To find out if .swp exist:

  • With the target file open in vim, execute :sw to get current swap file.
  • Check the directory that the current swap file is contained in.
  • Then check if directory contains a swap file with the name of the open file and has an extension of .swp.

The snippet above follows the same process, but remove all swap files.

Hope this helps.

nate

Posted 2012-09-27T14:47:47.173

Reputation: 111

0

From the manual :h swap:

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

JosephHarriott

Posted 2012-09-27T14:47:47.173

Reputation: 195

Warning... that probably isn't name of the file you need to delete. Until you've delete the .swp file, the :sw command will probably give you the name of the .swo file - which is not what you want to delete. – Lqueryvg – 2018-04-01T11:54:48.577

0

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

Kaleem Ullah

Posted 2012-09-27T14:47:47.173

Reputation: 101

1I 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 – 2015-09-24T16:00:54.943

-1

You can use,

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

Example:

rm -f .rc.local.swp

RizonBarns

Posted 2012-09-27T14:47:47.173

Reputation: 101

This technically works, but it's nowhere near as easy at the current accepted answer. – Peeja – 2015-03-18T14:36:00.670

Plus <ext> is redundant here as a file name at least on Linux includes zero or more extensions. – Pavel Šimerda – 2017-08-13T06:52:54.940