Reputation: 21
Summary: I'm trying to get Vim to store backup files, swap files, and undo files in a directory relative to the working directory so that I can move those files with the working directory (such as on a USB stick).
Background:
I wrote a function that sets the backupdir
, directory
, and undodir
settings when Vim first starts up. I set the settings to initially look for relative paths in the working directory first (which would be on a USB stick), then look for a centralized directory in ~/vim/scratch_files/
, a centralized directory that I would rather not use if a relative path is available.
However, no matter what relative paths I put, Vim just insists on using the centralized path, whether testing on Linux or Windows Vim installations.
Research: I've checked the help files (in :help backupdir
), Vim Fandom pages, and Stack Exchange, and have searched for solutions and couldn't find anything that makes it work.
EXAMPLE: Here is a minimally working example of what I'm talking about. I have a line in my .vimrc that adds a file, say for instance scratchfiles.vim
, to the runtimepath variable with the following code:
function! SetLocalScratchFiles()
" Turn the settings on
set backup
set writebackup
set swapfile
if has("win32") || has("win64")
" The // gets replaced with a %-substituted path for a filename in the destination scratch file in Windows as per :h backupdir (i.e., C%%path%to%file.txt.swp)
set backupdir^=.\\_scratch\\backup//,.\\.scratch\\backup//,$HOME\\.vim\\scratch_files\\backup//,.
set directory^=.\\_scratch\\swap//,.\\.scratch\\swap//,$HOME\\.vim\\scratch_files\\swap//,.
else
set backupdir^=./_scratch/backup//,./.scratch/backup//,~/.vim/scratch_files/backup//,.
set directory^=./_scratch/swap//,./.scratch/swap//,~/.vim/scratch_files/swap//,.
endif
" Undo Directory:
if has('persistent_undo')
if has("win32") || has("win64")
set undodir^=.\\_scratch\\undo//,.\\.scratch\\undo//,$HOME\\.vim\\scratch_files\\undo//,.
else
set undodir^=./_scratch/undo//,./.scratch/undo//,~/.vim/scratch_files/undo//,.
endif
set undofile
endif
endfunction
autocmd VimEnter * call SetLocalScratchFiles()
It's supposed to (a) look for local directory relative to the present working directory called ./_scratch/
for subdirectories /backup/, /swap/ and /undo/. Then, if that's not found, (b) look for the same subdirectories in ./.scratch/
. THEN if that's not found, use ~/.vim/scratch_files/
.
QUESTION:
The problem is that no matter how many different types of relative paths I feed these settings, Vim always picks the one centralized directory. How can I get it to recognize these relative paths first? Is there something wrong with the way I've formatted the paths, perhaps?
Upvotes: 1
Views: 779
Reputation: 21
The problem was putting these settings inside of a function that is called by autocmd VimEnter * ...
-- after removing the first and last two lines from the previous code, the local directories are recognized and the correct relative paths are set. I believe has to do with the order in which Vim handles autocmd's. It must fire those off after looking at the vanilla settings in the vimrc.
The bottom line is not to set backupdir
, undodir
, or directory
via VimEnter
since these need to be set sooner than when VimEnter
is called.
Upvotes: 1