Move Vim .swp files

Vim has this annoying habit of creating .swp files in your current working directory when you're editing a file. I found a better way (inspired by this stackoverflow thread). To change this, create three new folders to house all your .swp files...

mkdir -p ~/.vim/{backup_files,swap_files,undo_files}

Then add the following lines to your .vimrc fileā€¦

set backupdir=~/.vim/backup_files//
set directory=~/.vim/swap_files//
set undodir=~/.vim/undo_files//

See the docs for more info. 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_files/. If you specify ~/.vim/swap_files// 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 %).

Presto! Now all the temp files Vim creates will be stored in these directories and won't clutter up your working directory.

Post A Twitter Response To This Blog Post

To: @mattccrampton