Skip to content

Instantly share code, notes, and snippets.

@mllg
Last active March 19, 2021 04:16
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 mllg/5353184 to your computer and use it in GitHub Desktop.
Save mllg/5353184 to your computer and use it in GitHub Desktop.
Utility vim function to clean up vim's undo files (or others).
function Tmpwatch(path, days)
let l:path = expand(a:path)
if isdirectory(l:path)
for file in split(globpath(l:path, "*"), "\n")
if localtime() > getftime(file) + 86400 * a:days && delete(file) != 0
echo "Tmpwatch(): Error deleting '" . file . "'"
endif
endfor
else
echo "Tmpwatch(): Directory '" . l:path . "' not found"
endif
endfunction
" undofile part of your .vimrc
if exists("+undofile")
    if !isdirectory($HOME . '/.vimundo')
        mkdir($HOME . "/.vimundo")
    endif
    set undodir=~/.vimundo//
    set undofile
    set undolevels=1000
    set undoreload=10000
" remove undo files which have not been modified for 31 days
call Tmpwatch(&undodir, 31)
endif
@rlue
Copy link

rlue commented Jun 18, 2017

Cool! I used this.

I think L17 should be call mkdir though.

@cwfoo
Copy link

cwfoo commented Mar 19, 2021

In order to correctly handle filenames containing newline characters, you should use globpath(l:path, '*', 1, 1) instead of split(globpath(l:path, "*"), "\n").

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment