Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Vim's persistent undo?

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

like image 831
duckworthd Avatar asked Apr 18 '11 08:04

duckworthd


People also ask

How do I undo in vi mode?

Undo Changes in Vim / Vi Press esc to return to normal mode. Any character typed in normal mode will be interpreted as a vim command. Press u, :u, or :undo to undo the last change (entry).

How to undo in Vim after save?

Undo changes in vim / ViType u to undo the last change. To undo the two last changes, you would type 2u . Press Ctrl-r to redo changes which were undone.


2 Answers

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 
like image 145
Matthias Braun Avatar answered Oct 13 '22 11:10

Matthias Braun


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.

like image 31
Juan Lanus Avatar answered Oct 13 '22 10:10

Juan Lanus