Using Vim's persistent undo?

VimUndoUndo Redo

Vim Problem Overview


One of the new features in Vim 7.3 is 'persistent undo', which allows for the undotree to be saved to a file when exiting a buffer.

Unfortunately, I haven't quite been able to get it properly enabled, or I must be using it wrong. Here's what I've tried so far:

I added the following to ~/.vimrc

set undofile                " Save undos after file closes
set undodir=$HOME/.vim/undo " where to save undo histories
set undolevels=1000         " How many undos
set undoreload=10000        " number of lines to save for undo

After this, I supposedly should be able to open any file, edit it, then save-close it, and when I open it again I should be able to undo/redo as if I'd never left. Unfortunately, this doesn't seem to be the case, as no undofile is ever written.

Notes:

  1. I'm on Win 7 using Vim 7.3 from the Vim without cream project. Persistent undo is baked-in.

  2. $HOME/.vim/undo exists on my file system

Vim Solutions


Solution 1 - Vim

Put this in your .vimrc to create an undodir if it doesn't exist and enable persistent undo. Tested 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

Solution 2 - Vim

I tried this in my _gvimrc:

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

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

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

I had to create the directory.

Solution 3 - Vim

I suppose $HOME doesn't work as advertised.

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

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

Solution 4 - Vim

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

Solution 5 - Vim

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

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

I have no idea why

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionduckworthdView Question on Stackoverflow
Solution 1 - VimMatthias BraunView Answer on Stackoverflow
Solution 2 - VimJuan LanusView Answer on Stackoverflow
Solution 3 - VimseheView Answer on Stackoverflow
Solution 4 - VimjoharrView Answer on Stackoverflow
Solution 5 - VimlkreinitzView Answer on Stackoverflow