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.
Asked
Active
Viewed 3.1k times
34

Philip Kirkbride
- 21,381
- 38
- 125
- 225

brokenfoot
- 11,083
- 10
- 59
- 80
-
1Restore from a backup. – choroba Jul 29 '13 at 23:53
-
@choroba , precisely, that's what I need to know, how do I do that? – brokenfoot Jul 30 '13 at 00:10
-
2Do you have persistent undo turned on? (Most likely not but its on option worth looking into for future use) Take a look at `:h persistent-undo` – FDinoff Jul 30 '13 at 00:12
-
@FDinoff : I didn't had that ON, just now added the following in my .vimrc: `set undofile` `set undodir=/home/
/.vimundo/` – brokenfoot Jul 30 '13 at 00:16 -
If you don't see a filename~ previous version of the file in the directory, your .vimrc may have bdir or backupdir set to something like ~/.vimbackup – stevesliva Feb 27 '15 at 21:46
1 Answers
60
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 the 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 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.

gerrard00
- 1,628
- 14
- 17

brokenfoot
- 11,083
- 10
- 59
- 80
-
8Notice that you have to make sure the directory exists to make it work. – Searene Jun 22 '17 at 00:08
-
1I'm not sure why this answer was accepted. I have the same situation as asked in the question, but I still don't understand how it solves the problem. The setting seems to work only for new files/changes. – ka3ak Sep 05 '18 at 15:07
-
1You can make sure the directory exists by adding a statement `silent !mkdir -p ~/.vim/undo` inside the if-block. This solution assumes you have write permissions to create the directory `~/.vim/undo`. – Miron Veryanskiy Apr 03 '19 at 21:24