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.
let l:output = ''
redir => l:output
silent exec ':sw'
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
echo "swap file removed: ".l:swap_file
" Reset swap file extension to `.swp`.
set swf! | set swf!
echo "Reset swap file extension for file: ".expand('%')
command! DeleteFileSwaps :call DeleteFileSwaps()
Once encounter with the predicament, one can execute
This is great if combine with
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
- 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
The snippet above follows the same process, but remove all swap files.
Hope this helps.