What's the easiest way to delete Vim swapfiles I've already recovered from?
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.
214 Answers
A slightly simpler way
From the terminal emulator:
$ vim filenameThen 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).
7To 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]" -deleteSome inspiration and thoughts came from Removing junk .swp filesat Google Groups. This will delete all files
whose names end with .swk, .swl, .swm, .swn, .swo, or .swpin 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.swp. Gary 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
The original author of this post says,\*.sw[nop]or even
\*.sw?would be more thorough.
'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]" \) -deletewhich matches all (lower-case) three-letter extensions ranging from
.saato.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 edithello.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., withfind . -type f -name ".*.sw[klmnop]" -delete
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.
4Btw. 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?
- If so, swap to the window where we are editing that file.
- Otherwise, if swapfile is older than file itself, just get rid of it.
- Otherwise, open file read-only so we can have a look at it and may save it.
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 itNow just press d :)
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.
3Here 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 :DeleteFileSwapsThis 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
:swto 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.
These files were annoying for me too, but i set option in .vimrc - set noswapfileto prevent vim create it and, instead, keeping files in memory.
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]" \) -deleteBe 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.
From the manual :h swap:
1You can see the name of the current swap file being used with the command:
:sw[apname]
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 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.txtthen 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.txtthen (E) or (R) then
:!rm %:p:h/.%:t.sw* vi filenamerecover↩
:w:edelete
You can use,
rm -f .<filename>.<ext>.swpExample:
rm -f .rc.local.swp 2