Skip to content

Instantly share code, notes, and snippets.

@sirlancelot
Created September 29, 2010 05:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sirlancelot/602340 to your computer and use it in GitHub Desktop.
Save sirlancelot/602340 to your computer and use it in GitHub Desktop.
Portable Vim function to delete old backup files.
" I used to run call system('find ~/.vimbackup -mtime +14 -exec gvfs-trash "{}" \;')
" but now have created a more portable version since I work on Windows, Mac, & Linux.
set backup
set nowritebackup
set backupcopy=yes
set backupdir=$HOME/.vimbackup
set directory=$HOME/.vimswap,./
" Timestamp the backups
au VimrcHooks BufWritePre * let &backupext = '~' . localtime()
au VimrcHooks VimLeave * call <SID>DeleteOldBackups()
function! s:DeleteOldBackups() " {{{2
" Delete backups over 14 days old
let l:Old = (60 * 60 * 24 * 14)
let l:BackupFiles = split(glob(&backupdir."/*", 1)."\n".glob(&backupdir."/.[^.]*",1), "\n")
let l:Now = localtime()
for l:File in l:BackupFiles
if (l:Now - getftime(l:File)) > l:Old
call delete(l:File)
endif
endfor
endfunction " }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment