Vim undo: undo changes after file write

brokenfoot picture brokenfoot · Jul 30, 2013 · Viewed 22.9k times · Source

In my code on vim, I did a lot of changes and then did a ZZ (save and exit). But then I realized I didn't need those changes. Is there a way I can get back to the state before doing those changes using from some buffer where that data still might be stored. I haven't made any changes after the save & exit.

Answer

brokenfoot picture brokenfoot · Jul 30, 2013

There's persistent undo option in vim, :h persistent-undo
Note: It was introduced in VIM 7.3 version, so for earlier versions, it will not work.

It can be turned on by placing following text in your .vimrc:

if has('persistent_undo')      "check if your vim version supports it
  set undofile                 "turn on the feature  
  set undodir=$HOME/.vim/undo  "directory where the undo files will be stored
  endif     

Note: Before enabling this option, whatever that was lost, remains lost.
After enabling the option, you will be able to do subsequent undo/redo on whatever was added/deleted after enabling the option.