kirypto
kirypto

Reputation: 139

Workaround for Vim's inability to support `backupdir` full paths

Recently I tried to switch to having backup and swap files in a directory other than the working directory. With swp files, you can just put double-slashes at the end of the path:

set directory=~/.vim/swp//

However, this does not work with backupdir. I found an answer with a workaround however:

autocmd BufWritePost * :execute ':w! ' ."$HOME/.vim/backups/" . escape(substitute(expand('%:p'), "/", "%", "g"), "%")

I have a few questions regarding this workaround:

Thanks

Upvotes: 0

Views: 222

Answers (1)

boobie_goodness
boobie_goodness

Reputation: 104

My workaround was

" this is a default. to be updated per file type
autocmd BufNewFile,BufNew,BufRead * let b:buDir = "/work/BACKUP/vimBackups/all/"

then later, on a per-file-type basis I did

let b:buDir = "/work/BACKUP/vimBackups/text/"

or

  let b:buDir = "/work/BACKUP/vimBackups/coding/"

Then, I call my backup on every exit

" the backup command
" currently, overwrite whatever is there: a new file will be made each second
" if necessary
autocmd VimLeavePre,BufWritePre * silent execute ":write! >> ".b:buDir."vim_bu_".strftime('%Y-%b-%d__%H:%M:%S')."_".b:buFilename.".bu.txt "

Then, in a daily clean-up script, run from cron (anacron), I delete out my old backups

# vim backups older than a month old
find $VIM_BU_DIR -not -newerat "1 month ago" -delete    ;

Upvotes: 1

Related Questions