Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim undo: undo changes after file write

Tags:

undo

vim

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.

like image 708
brokenfoot Avatar asked Jul 29 '13 23:07

brokenfoot


People also ask

What is the command to undo what you just did in vi?

Undoing the Previous Command If you make a mistake in vi or if you just change your mind after an operation is completed, you can undo your last command by pressing u immediately after the command. You do not need to press Esc after you type u . By pressing u a second time you undo the undo.

How many times can you use the Vim undo command?

2 Answers. Show activity on this post. You have only 1 level of undo if you are in Vi compatible mode. You are missing out on a number of features by being in 'compatible' mode.

Which command is used to undo the previous steps?

To undo an action press Ctrl+Z. If you prefer your mouse, click Undo on the Quick Access Toolbar. You can press Undo (or CTRL+Z) repeatedly if you want to undo multiple steps.


1 Answers

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.

like image 118
brokenfoot Avatar answered Oct 17 '22 11:10

brokenfoot