Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"backupdir" subfolders in VIM

Tags:

vim

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

like image 307
fieldtensor Avatar asked Nov 10 '22 03:11

fieldtensor


1 Answers

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.

like image 125
FDinoff Avatar answered Nov 15 '22 05:11

FDinoff