Skip to content

Instantly share code, notes, and snippets.

@nickiaconis
Created May 11, 2017 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickiaconis/dfe0dc48d365856c3f99cc053f7750c1 to your computer and use it in GitHub Desktop.
Save nickiaconis/dfe0dc48d365856c3f99cc053f7750c1 to your computer and use it in GitHub Desktop.
Workaround for `set backupdir=~/.vim/backup//`
" from http://stackoverflow.com/a/38479550/2085526
" === BACKUP SETTINGS ===
" turn backup OFF
" Normally we would want to have it turned on. See bug and workaround below.
" OBS: It's a known-bug that backupdir is not supporting
" the correct double slash filename expansion
" see: https://code.google.com/p/vim/issues/detail?id=179
set nobackup
" set a centralized backup directory
set backupdir=~/.vim/backup//
" This is the workaround for the backup filename expansion problem.
autocmd BufWritePre * :call SaveBackups()
function! SaveBackups()
if expand('%:p') =~ &backupskip | return | endif
" If this is a newly created file, don't try to create a backup
if !filereadable(@%) | return | endif
for l:backupdir in split(&backupdir, ',')
:call SaveBackup(l:backupdir)
endfor
endfunction
function! SaveBackup(backupdir)
let l:filename = expand('%:p')
if a:backupdir =~ '//$'
let l:backup = escape(substitute(l:filename, '/', '%', 'g') . &backupext, '%')
else
let l:backup = escape(expand('%') . &backupext, '%')
endif
let l:backup_path = a:backupdir . l:backup
:silent! execute '!cp ' . resolve(l:filename) . ' ' . l:backup_path
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment