139

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.

2
  • 2
    See here vi.stackexchange.com/questions/13518/… "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 vi.stackexchange.com/questions/13518/…
    – zenaan
    Oct 1, 2019 at 0:50
  • 1
    this can happen, for instance, if you did <C-z> because some silly mapping used this but the mapping was commented out / removed. In which case, you should do fg from terminal to get back that vim session. Jan 28, 2021 at 11:17

14 Answers 14

123

A slightly simpler way

From the terminal emulator:

$ vim filename

Then inside vim choose recover, and if you want to delete the swap you write (save) and then call this command:

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

...and choose delete this time.

A benefit to this approach is that it won't automatically overwrite a working file, if the recovery turned out corrupt (though admittedly I've never had that happen before).

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

7
  • 38
    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, 2015 at 17:24
  • 8
    @Arne: From my experience that usually means the file is currently opened in another instance of vim?
    – miyalys
    Oct 31, 2015 at 17:32
  • 4
    The [D]elete option only appeared after I updated to Vim 7.4
    – jmlarson
    Mar 30, 2017 at 15:09
  • 19
    For 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
    Apr 1, 2018 at 12:34
  • 4
    For me, the workflow was: vim filename, choose [R]ecover, :w to save, :e, then choose [D]elete.
    – Ben Davis
    Aug 1, 2021 at 15:30
62

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
    
2
  • 1
    This does not answer the question at all. OP asks about deleting a specific file's swap file they know they recovered from. Here, you're not only deleting all the swap files in an entire directory for no clear reason, and moreover not even checking if your recovered from any of them or not. That answer is a data loss hazard.
    – adamency
    Apr 16, 2023 at 16:18
  • @adamency, check the title again, it's "What's the easiest way to delete Vim swapfiles", note it's swapfiles plural not singular. Furthermore they ask for the easiest way. To say it does not answer the question at all is clearly false. To add a warning about data loss hazard is possibly a productive contribution to the conversation.
    – Shwaydogg
    May 22, 2023 at 21:31
23

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.

5
  • 6
    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, 2013 at 19:41
  • 1
    ls: cannot access +A: No such file or directory
    – Jonathan
    Jun 17, 2015 at 15:15
  • this is what i need
    – devugur
    Nov 3, 2018 at 7:12
  • This is probably what most people coming here are looking to do.
    – GWR
    Feb 24, 2021 at 15:07
  • This answer completely misses the point that swapfiles are by default and most likely in practice NOT in the same directory as the file itself. You're not providing any help, and the only thing you achieve is patronizing OP and other people looking for a solution.
    – adamency
    Jul 1, 2023 at 15:08
8

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

5
  • 12
    Really? I mean, I knew that. But that's stupid. It's certainly not "simple".
    – Peeja
    Sep 27, 2012 at 15:12
  • 1
    By the way, what's the difference between quit and abort?
    – trusktr
    Oct 3, 2013 at 5:10
  • 7
    there is no Delete option in my case. what should I do? (I saved, quit, and reopened)
    – Paschalis
    Dec 4, 2014 at 10:14
  • 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 Jul 17, 2017 at 23:30
  • Use vim -r filename and then Delete will be an option. Jun 3, 2021 at 22:48
8

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.
1
  • 9
    Can anyone tell me why vim cannot have sensible behavior by default, without plugins? Aug 13, 2017 at 6:46
3

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.

3
  • 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, 2013 at 14:33
  • 1
    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, 2013 at 5:09
  • 3
    Most 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. Aug 13, 2017 at 6:49
2

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.

3
  • According to the OP, swapfiles are useful, so turning them off might not be the best option. Jun 24, 2016 at 12:14
  • Don't turn off swap files. If your machine crashes, you'll lose your work.
    – Lqueryvg
    Apr 1, 2018 at 11:47
  • 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
    Apr 1, 2018 at 19:18
2

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.

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.

1
  1. vi filename

  2. recover

  3. :w

  4. :e

  5. delete

0

From the manual :h swap:

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

1
  • 1
    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
    Apr 1, 2018 at 11:54
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
1
  • 1
    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, 2015 at 16:00
0

I often don't see the (D) Delete it option. I guess because I'm on *nix and the .swp file is left over from some crashed or killed vim process. So, if I just want to delete all .sw* files after opening my file I'll issue:

vim my-file-in-current-directory.txt

then hit (E) Edit anyway or (R) Recover

and then once in vim issue:

:!rm .%.sw*

If you're not in the same directory as the file you can still do this, but it's not so easy to remember or type out:

vim my/file/with-really-long-name/in-a-directory-far-away.txt

then (E) or (R) then

:!rm %:p:h/.%:t.sw*
-1

You can use,

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

Example:

rm -f .rc.local.swp
2
  • This technically works, but it's nowhere near as easy at the current accepted answer.
    – Peeja
    Mar 18, 2015 at 14:36
  • Plus <ext> is redundant here as a file name at least on Linux includes zero or more extensions. Aug 13, 2017 at 6:52

You must log in to answer this question.

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