2

I’m usually fine with swap files being created in the same directory as the files being edited—I have the directory setting left on its default value. I have one project, though, with “file watching” functionality that gets confused by swap files.

I notice that directory is a global setting (i.e. you can’t set it on a per-buffer basis). Is there some workaround so that I can change where swap files are put only when I’m editing files within a certain directory?

statox
  • 49,782
  • 19
  • 148
  • 225
bdesham
  • 2,983
  • 2
  • 18
  • 20

2 Answers2

2

If you're willing to edit those files only, using a separate instance of vim, you can do a vim -n <filename> which tells vim to use memory only, no swap files for editing.

Alternatively, you could create a .vimrc file in that particular directory, containing the following:

source ~/.vimrc
directory=~/tmp

then start vim within that directory via vim -u <vimrc> <filename>

Herb
  • 1,213
  • 1
  • 14
  • 28
0

I haven't thought this 100% through, but maybe you could look into it.

Why not do a chain of autocomands, where BufWinEnter adds a WinEnter autocommand, which in turn changes directory and stores the original value in a buffer-local variable, and a WinLeave aoutocommand that changes it back, and a BufWinLeave au. that removes the previous two? It's kind of a mess, but if you do it right, it should work well.

(please tell me if this needs further explaining)

EDIT: I just realized this depends on whether or not vim cares about swap files of bufers you aren't currently editing, so it really comes down to testing. If you're willing to spend the, I don't know, probably no more than 30 minutes to try it, please be sure to tell us if it works :)

DarkWiiPlayer
  • 196
  • 1
  • 8