Currently I have this in my VIM config file:
set backupdir=$HOME/Documents/vim-backup/
This mostly works fine, but I'm bothered by how it puts all files into the same directory. That would be especially annoying if, for example, one were to be editing multiple README.txt files at the same time, since their backups would all clobber each other.
Is it possible to have VIM store backups in subdirs of the backupdir? For example, if I were editing C:\Users\patrick\Desktop\project-1\foo.c
, then I would want the backup file to go into something like C:\Users\patrick\Documents\vim-backup\Users\patrick\Desktop\project-1\foo.c
You could do something like this.
set backupdir=$HOME/.vim/backup
let s:backupdir = &backupdir
function! SetBackupDir()
exec 'set backupdir=' . s:backupdir . expand('%:p:h')
if !isdirectory(&backupdir)
call mkdir(&backupdir, "p")
endif
endfunction
This will automatically change the backupdir
setting to be $HOME/.vim/backup/path/to/filedirectory
before vim saves the file. Vim will then write the backup file to this directory with a trailing ~
. If you want to change the ~
you can use change backupext
setting. If the directory doesn't exist it will be created.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With