[ACCEPTED]-Using Vim's persistent undo?-undo-redo

Accepted answer
Score: 58

Put this in your .vimrc to create an undodir if it doesn't 2 exist and enable persistent undo. Tested 1 on both Windows and Linux.

" Put plugins and dictionaries in this dir (also on Windows)
let vimDir = '$HOME/.vim'

if stridx(&runtimepath, expand(vimDir)) == -1
  " vimDir is not on runtimepath, add it
  let &runtimepath.=','.vimDir
endif

" Keep undo history across sessions by storing it in a file
if has('persistent_undo')
    let myUndoDir = expand(vimDir . '/undodir')
    " Create dirs
    call system('mkdir ' . vimDir)
    call system('mkdir ' . myUndoDir)
    let &undodir = myUndoDir
    set undofile
endif
Score: 8

I tried this in my _gvimrc:

" Persistent undo
try 
    set undodir=C:\vim\undodir
    set undofile
catch
endtry

It started working 2 as advertised when I deleted the try-catch 1 bracket, thus:

" Persistent undo
set undodir=C:\vim\undodir
set undofile

I had to create the directory.

Score: 3

I suppose $HOME doesn't work as advertised.

On 3 my system, :echo $HOME shows H:\, but : e $HOME/ says: ~/ invalid 2 filename.

You could try with an absolute 1 path to see whether it cures it

Score: 3

If you are looking for %TEMP%.(filename).un~ you 3 won't find it

The filename will be something 2 line C%%Users%%(username)%_vimrc

I have 1 no idea why

Score: 1

This now works as expected: file.txt open in a Vim 5 7.4 buffer on Windows 7, :setlocal undofile, then save a change 4 to the buffer, and the undofile .file.txt.un~ is created 3 alongside because :set undodir? reports that "undodir=." by 2 default - ie no need to specify this manually. You 1 can also :set undofile in a modeline.

More Related questions