Vim: how to trace back all changes done to file during the day

backupfilessessionvim

Does anybody know how to setup vim (the text editor) in such a way to make it possible to trace back all the changes done to a file within a day? I need it for the cases when I have accidentially modified a subroutine and would like to return it to it's initial state, as it was at the start of the day.

I usually use svn to keep the most recent version of my source code in a global repository. But sometimes I need to return to the state the code was in in between of two svn commits.

Update:

To enable persistent undo create the undo directory, e.g. ~/.vim/undodir, and place the following settings into the .vimrc file:

set undodir=~/.vim/undodir
set undofile
set undolevels=1000
set undoreload=10000

Best Answer

With a large enough value of 'undolevel', Vim should be able to undo the whole day's changes. If you quit Vim in between, you also need to enable persistent undo by setting the 'undofile' option.

Vim captures not just a sequential list of commands for undo, but actually a tree of all changes. It also has several commands around undo (cp. :help undo-branches); to go back to the state at the beginning of the day,

:earlier 12h

is a good candidate.

There are also plugins like Gundo and undotree.vim, which visualize the undo tree and allow to navigate it.

If navigating the undo tree sounds too cumbersome (e.g. because you make a lot of changes throughout the day), you can also add a lightweight version control system (in addition to Subversion), like my writebackup plugin, and explicitly create a backup at certain times.