Alex Rivera | Logout

Disabling swap file creation in Vim

Asked 2009-05-04T20:28:23.067
261

Is there a way to disable the creation of .swp files in Vim? Or, at least create them in one place so that I can find and delete them easily?

I find swap files especially annoying when I copy the parent directory while editing at the same time. Of course I know that I can use find -exec to find and delete them, but I want a more practical solution.

Edit
Report

2 Answers

185

Set the following variables in .vimrc or /etc/vimrc to make vim put swap, backup and undo files in a special location instead of the working directory of the file being edited:

set backupdir=~/.vim/backup//
set directory=~/.vim/swap//
set undodir=~/.vim/undo//

Using double trailing slashes in the path tells vim to enable a feature where it avoids name collisions. For example, if you edit a file in one location and another file in another location and both files have the same name, you don't want a name collision to occur in ~/.vim/swap/. If you specify ~/.vim/swap// with two trailing slashes vim will create swap files using the whole path of the files being edited to avoid collisions (slashes in the file's path will be replaced by percent symbol %).

For example, if you edit /path/one/foobar.txt and /path/two/foobar.txt, then you will see two swap files in ~/.vim/swap/ that are named %path%one%foobar.txt and %path%two%foobar.txt, respectively.

answered 2013-03-09T23:31:00.843
7

You can set backupdir and directory to null in order to completely disable your swap files, but it is generally recommended to simply put them in a centralized directory. Vim takes care of making sure that there aren't name collissions or anything like that; so, this is a completely safe alternative:

set backupdir=~/.vim/backup/
set directory=~/.vim/backup/
answered 2010-09-26T22:16:10.130

Your Answer