I have this in my .vimrc:

set directory=~/.vim/tmp-backup-swapfiles

This directory is read- and writable by me, sensibly enough. It is part of the local filesystem and never inaccessible.

However, vim seems to often ignore this and instead use ~/tmp. I've sometimes thought it does this with files from a remote mounted filesystem, but since I do that much of the time it is hard to tell. Also, there are inconsistencies -- for example, right now I have gvim (7.4.475) open with a local file who's swap turns out to be in ~/tmp and a remote file whose swap is in ~/.vim/tmp-backup-swapfiles. It is easy to see they are both still in play by making a change to the buffer and checking the timestamp. I also have a source and header file from the same location open, but the .hpp.swp is in ~/tmp and the .cpp.swp is in ~/.vim/tmp-backup-swapfiles :\

This gets irritating when I have to clean up for whatever reason, or fail to, and get told there's suddenly a swapfile from last week in play on something I've been editing since. What can I do to force it to just use the one directory specified to ensure there's no confusion?



:set dir?

to see which directories are used to store your swap files. If you insist on having just one directory, you could use

set directory=
set directory=~/.vim/tmp-backup-swapfiles//

Using double trailing slashes in the path tells Vim to create swap files using the whole path of the files being edited to avoid collisions.

  • Hmmm -- the output from :set dir? seems inaccurate; right now I get ~/tmp,/var/tmp,/tmp, but there's a .swp file in ~/.vim/tmp-backup-swapfiles that is obviously being used (when I change the buffer, the timestamp on that file is immediately updated). I'll try your suggestion; I'm guessing the first set directory= is to clear any defaults? To clarify further, are you saying that vim only resorts to other directories when I load two files with the same name but different paths? – goldilocks Oct 15 '15 at 1:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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