On Thursday, September 13, 2012 10:32:22 AM UTC-5, Karthick wrote:
>
> > I have some slow network shares, so I gave this idea a try to prevent using them for swap files. It seems successful so far, but beware of the crash I observed here:
https://groups.google.com/d/topic/vim_dev/ZcJEKqNsVRI/discussion
>
> >
>
> > Here's the code I came up with:
>
> >
>
> > " Make slow network shares on Windows prefer not to use the file's directory
>
> > " for swap files
>
> > if has('win32')
>
> > set noswapfile
>
> > autocmd BufReadPre * setlocal noswapfile
>
> > autocmd BufEnter {Z,U,W}:{/,\\}* set dir-=. dir+=.
>
> > " nested autocmd to enable swap files so SwapExists autocmds fire, too
>
> > autocmd BufEnter * nested setlocal swapfile
>
> > autocmd BufLeave {Z,U,W}:{/,\\}* set dir-=. dir^=.
>
> > endif
>
> I think the basic assumption here is BufReadPre triggers execute
>
> before swap file is created?
>
>
Yes, that was my assumption, since it's the earliest event I know of when reading a buffer, and :help 'swapfile' indicates that it is buffer-local to allow you to not use swap files when you want to protect confidential information.
>
> I tried to validate that with the following:
>
>
>
> au BufReadPre TEMPTST call Check()
>
> function Check()
>
> sleep 10
>
> endfunction
>
>
>
> :e TEMPTST
>
> <type some chars>
>
> " Now delete the buffer
>
> :bd
>
>
>
> " Reload buffer to test the autocmd
>
> :e TEMPTST
>
> BufReadPre will now trigger. I was monitoring the directory (in a file
>
> explorer) and saw a swap file being created before the Check()
>
> function returns.
Interesting, so I suppose my assumption was invalid. Regardless, since the BufReadPre event triggers before the buffer is read, the swap file created before BufReadPre cannot contain anything from the file itself. I'm not sure what it can contain at that point, actually. It now makes more sense to me why the BufReadPre autocmd is necessary. I had a problem when editing a file the second time, because Vim remembered the 'swapfile' setting from the first time, and I guess it was creating a swap file before the BufReadPre event using the wrong 'dir' setting, since BufEnter hadn't fired yet. Luckily setting 'swapfile' seems to do the same check as if you're editing a file with it already set, so it seems to work as intended. I can edit files much faster on my network shares now, at least, probably because a bunch of text need not be written to the swap file temporarily created before BufReadPre.
I used the :swapname command, by the way, to determine whether my autocmds were properly setting the location of my swap file.
If you need to NEVER create a file in your Dropbox folder, perhaps you could reverse the logic. Set up 'dir' as you want it for Dropbox in your .vimrc, and somehow set it to how you want it otherwise for all other locations using autocmds.