4

When I'm writing over webdavfs vim temporary doesn't handle temporary files properly:

ll -a
ls: cannot access .htaccess.swp: No such file or directory
ls: cannot access .htaccess.swx: No such file or directory
total 15196
drwxrwxr-x  2 sergio sergio     448 Ago 22 04:48 .
drwx------ 12 sergio sergio     528 Ago 22 04:47 ..
-rw-rw-r--  1 sergio sergio      21 Ago 22 04:48 .htaccess
-?????????  ? ?      ?            ?            ? .htaccess.swp
-?????????  ? ?      ?            ?            ? .htaccess.swx

How I can open a file with vim setting swap files in other directory ?

Note I just want this happens when I'm under webdav directories.

UPDATE : yes,

vim "+set noswapfile" .htaccess 

Could be a solution but somehow the files .htaccess.swp .htaccess.swx still appears in webdav directory ...

Update 2016-03-16: still don't have an working solution , Editing like Remote Files may be a solution, anyone ? other solution seems to be https://github.com/kana/vim-metarw

Update 2016-09-11: something like this could work , but seems works even better edit ~/.davfs2/davfs2.conf and set "use_locks 0"

if expand("%:p:h") =~ '^/home/sergio/webdav'
     echo "in webdav dir"
     set directory=/tmp
     set backupdir=/tmp
     colorscheme desert
endif
  • 3
    set noswapfile ? – Alex Kroll Aug 22 '15 at 9:06
  • Not sure if I understand what the problem is exactly? Statox's answer seems to be a solution; specifically, setting the directory and backupdir so that Vim stores all those files in ~/.vim/tmp or some such ... Didn't this work for you? Is this not an option? – Martin Tournoij Mar 16 '16 at 18:38
  • The Statox's solution not worked, still leave temporary files over webdav, if set directory=~/tmp and set backupdir=~/tmp in .vimrc still got one empty file over webdav – Sérgio Mar 17 '16 at 12:01
5

The options you need are directory and backupdir they allow you to set the directory where swap files and backup files are stored.

The second part of the problem is how to detect that you are editting files in a webdav directory to set these options. I would have tried an autocommand to do so but according to this discussion it seems to be pretty hard: From what I have understood, they didn't managed to trigger an autocommand before the swap file is created (so changing the values of the swap directory is useless).

So what I could suggest to ease the process is either to definitely set the directory options for all your files or insert the following lines in your .vimrc:

let g:directory_save  = &directory
let g:bdirectory_save = &backupdir
let g:currentDirState = 0

function! ChangeSwapDir()
    if (g:currentDirState)
        let g:currentDirState = 0
        set directory=~/tmp
        set backupdir=~/tmp
    else
        let g:currentDirState = 1
        execute "set directory=" . g:directory_save 
        execute "set backupdir=" . g:bdirectory_save
    endif
endfunction

command CSD call ChangeSwapDir()
  • The first 3 lines are used to save your default settings and initialize a variable which will tell us in what state the options are.
  • The function is used to switch the swap directory either to the default settings or to ~/tmp (of course you can change that to your prefered directory).

    Note that the function can be optimized and rewritten, I wrote it this way so that a newcomer to vimscript can understand it easily

  • And the last line allows you to call the function more easily with the command :CSD (you can add a mapping if you want).

The idea is to call it each time you'll begin to edit files over webdav to set the directory to ~/tmp and call it again when you finished to come back to default.

That is probably not as convenient as auto detecting that you are editing files in webdav directory but that seems to be a working workaround.

| improve this answer | |

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.