0

Due to my configuration, vim stores swap files in the same directory as the edited file. In general, that is ok, but not in the folder ~/ftp_mount (and all directories below). In this directory I mount an ftp server.

With

:set dir=/some/path

one can change the location in which vim stores the swap files (vim documentation). Is it possible to restrict this configuration to ~/ftp_mount and all directories below?

user3041714
  • 333
  • 1
  • 2
  • 11

2 Answers2

1

Disabling swapfile, setting directory, and enabling it back seems to work:

  autocmd BufNewFile,BufReadPre *
        \ setl noswapfile noundofile |
        \ if expand('%:p') =~ "/my/custom/path" |
        \   setl undodir=. directory=. |
        \ else |
        \   setl undodir=~/.cache/nvim/undo directory=~/.cache/nvim/swap |
        \ endif |
        \ setl swapfile undofile

I am matching *, not my path particularly, because I want it to work if I:

  1. Open a file under my custom path.
  2. Load a new file from elsewhere.
sencer
  • 345
  • 5
  • 17
-2

How about using an autocmd on the directory you want to apply this setting to (~/ftp_mount)

autocmd BufNewFile,BufRead ~/ftp_mount/* set dir=/some/path
Zach
  • 4,652
  • 18
  • 22
  • 1
    It would be great if this worked, but it seems that the `autocmd` happens after the swapfile has been set. You can see this by looking at the value of `:set directory` (which has the value set by the autocmd), and the value of `:swapname` (which has a value corresponding to the default value of `directory`). (edit) The answer is pretty old, perhaps the behaviour changed in a more recent version of Vim? – Mordac The Preventer Aug 24 '17 at 14:15