Cluttering Swaps and Backups that I never use are so annoying and takes up unnecessary space. Is there a way to delete them as one quits vim with :q and similar quitting commands?

  • are you sure you want to delete them and don't need them later on? That sounds a bit risky here... Feb 2, 2019 at 9:40
  • Well, I thought once a file is saved and I quit Vim, there's no real need for it to stay around.
    – Paul Kim
    Feb 2, 2019 at 9:42
  • Thanks to @Hotschke for correcting and adding tags to this question.
    – Paul Kim
    Feb 8, 2019 at 16:29

1 Answer 1


As Christian pointed out in his comment, there are some risks.

Swap-Files: They are deleted automatically on Vim exit. If you have swap files lying around, they are most likely from abnormal terminations (terminal close or reboot while Vim still running or crash). In that cases it might be worth to check the files.

To disable backup files, I would propose the following for your vimrc:

set writebackup
set nobackup

This settings writes a backup file while saving. After saving was successful, the backup file is deleted. This is a safety net in case Vim crashes while writing the file.

BTW: I had (rare) cases, where I was really happy that I had a backup file.

Well, and undo files is easy. Add the following to your vimrc:

set noundofile

Then undo is only held in memory and does not survive multiple edit sessions.

Note: Doing set noundofile at runtime, does not delete an existing undo file.

Another idea for backup files

Create a directory to hold all backup files. Let's say $HOME/vimbackup. Then add the following to your vimrc:

set backupdir=$HOME/vimbackup//

(The two trailing slashes are important! Even on Windows use //.)

Then Vim will create all backup files in that directory. Because of the trailing // It will create filenames that contain the entire path. Like %home%user%bin%script.sh~. Without the trailing slashes, it will just use plain file names, like script.sh~, which is bad if you have the same file name in different directories.

With this the backups are there, when you need them, but will not pollute your working directories. Well, you have to clean up from time to time.

(Unix only, I guess you are on Windows)

Regarding backup files: I let Vim write backup files. So some of my code directories are scattered with backup files. If I decide to get rid of them all (e.g. after I committed the stuff), I have a bash shell function rmbak:

    typeset opt OPTIND OPTARG depth="-maxdepth 1" action="-delete"
    while getopts ":rn" opt; do
        case "$opt" in
            r) depth='' ;;
            n) action='' ;;
                echo >&2 "Invalid option: -$OPTARG"
                echo >&2 "USAGE: rmbak [-rn]"
                echo >&2 "  -r recursive"
                echo >&2 "  -n print files but don't delete"
                return 1
    find . $depth -name \*~ -type f -print $action
  • 1
    Since backup files may be important later on I guess I'll find a way to organize them. That set noundofile is going straight to my _vimrc tho. Thanks!
    – Paul Kim
    Feb 2, 2019 at 13:47
  • 1
    @K.Paul See my update "Another idea for backup files"
    – Ralf
    Feb 2, 2019 at 14:01
  • 1
    Uh...one tiny footnote, that trailing // doesn't work on Windows (I guess it works on Unix) and I will comment a Win10-refined version of said code for future reference...once I find out what it is.
    – Paul Kim
    Feb 2, 2019 at 14:17
  • 1
    @K.Paul According to the docs it should work. In general you should also use forward slashes for paths on Windows too. Makes life easier. Like C:/vimbackup//. See :help 'backupdir'.
    – Ralf
    Feb 2, 2019 at 14:20
  • 1
    Oh. Whoops, my bad, the problem was with other backslashes in path; apparently I need to write two backslashes instead of one for Vim to recognize it as a backslash. // is right, people, and tread carefully around those backslashes.
    – Paul Kim
    Feb 2, 2019 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.