68

I'm working at a project on a remote server. I don't want to have the swap files on the server. I would like all swap files for Vim (and, of course, gVim) to be saved on the specified directory. How could I do that?

sica07
  • 4,616
  • 8
  • 34
  • 53
  • Here is a [link](http://stackoverflow.com/a/43286164/30038) on how to do it in Windows. – Vlad Bezden Apr 07 '17 at 19:59
  • Recommended answer for you to check out: https://vi.stackexchange.com/questions/11879/how-can-put-vimrc-and-viminfo-into-vim-directory/20067#20067 – DrBeco May 23 '19 at 14:51

2 Answers2

84

You can set the directory option to the location that you want vim to store its swap files, e.g.:

 mkdir -p $HOME/.vim/swapfiles  # this dir must exist vi does not create it

" $HOME/.vimrc
:set directory=$HOME/.vim/swapfiles//

I use trailing double path separators because, from the help docs:

For Unix and Win32, if a directory ends in two path separators "//" or "\\", the swap file name will be built from the complete path to the file with all path separators substituted to percent '%' signs. This will ensure file name uniqueness in the preserve directory.

Scott Stensland
  • 22,853
  • 10
  • 81
  • 88
maerics
  • 133,300
  • 39
  • 246
  • 273
  • 3
    This assumes you've set your HOME dir with: set HOME=~/ "or whatever location you like If you did not do this, use: set directory=~/.vim/swapfiles// "make sure that directory exists – Richard Feb 07 '16 at 09:00
  • 1
    How can i achieve this same thing on a PC running Windows 10? How would the path differ? – Casey Mar 07 '17 at 02:08
37

You might want to consider setting the backupdir options as well:

set backupdir=Z:\backups

That makes vim store backups in a specific location rather than in the current directory.

muru
  • 4,232
  • 30
  • 69
Benj
  • 29,382
  • 17
  • 72
  • 119
  • 1
    Thanks. I didn't know about this either. – sica07 Oct 29 '09 at 09:02
  • 3
    What if you edit two files with the exact same name that are in two different locations. Will there be a conflict in Z:\backups? – trusktr Mar 09 '13 at 21:12
  • 7
    @trusktr To avoid that problem you do this : :set dir=>D:\data\vimdata\vswaps// where the double slashes on the end of the path cause Vim to ensure the file in the swaps directory is always unique. This example, obviously, is from a windows enviroment but I believe the same applies for other platforms. – glaucon Aug 29 '13 at 00:28