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?

  • Usually, "file watching" tools have a mean to ignore files or file patterns.
    – Rolf
    Commented Nov 3, 2017 at 13:38

2 Answers 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

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


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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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