How to Undo in Vim / Vi text editor

last updated in Categories , ,

How do I undo a recent change in the vim text editor?

Vim / vi text editor remembers all last changes, such as adding or deleting text or block of code. You can quickly revert all changes in vim using undo mode. This page shows how to undo recent changes and editing in Vim / vi text editor running on Linux or Unix-like systems.

How to undo in vim

The commands are as follow to undo text and editing or deleting in vim. The syntax is:
u
OR
:u
OR
:undo
:undo N
:undo 3

To undo all latest changes on one line, run:
U

Undo changes in vim / Vi

  1. Press the Esc key to go back to the normal mode
    ESC
  2. Type u to undo the last change.
  3. To undo the two last changes, you would type 2u.
  4. Press Ctrl-r to redo changes which were undone. In other words, undo the undos. Typically, known as redo.

Examples

Start a new file:
vim demo.txt
Append the following text by pressing i
Linux macOS Unix nixCraft
Delete the second word by pressing x five times:
Linux Unix nixCraft
Now, undo that by pressing u five times:
Linux macOS Unix nixCraft
How to Undo in Vim or vi

List tree of changes

The following commands do not work in Vi and only works with Vim. It is also possible to branch off in undo. This happens when you undo a few changes and then make a new change. The undone changes become a branch. Type the following command to list the leafs in the tree of changes:
:undolist
Undolist to undo changes in Vim or Vi
Where,

  1. The “number” column is the change number. This number continuously increases and can be used to identify a specific undo-able change
  2. The “changes” column is the number of changes to this leaf from the root of the tree.
  3. The “when” column is the date and time when this change was made. For example, 8 seconds ago or hour, minute, seconds.
  4. The “saved” column specifies, if this change was written to disk and which file write it was. This can be used with the :later and :earlier vim commands.

:earlier, :later, g- and g+ commands

The syntax is as follows.

:earlier {count}	Go to older text state {count} times.
:earlier {N}s		Go to older text state about {N} seconds before.
:earlier {N}m		Go to older text state about {N} minutes before.
:earlier {N}h		Go to older text state about {N} hours before.
:earlier {N}d		Go to older text state about {N} days before.
:later {count}		Go to newer text state {count} times.
:later {N}s		Go to newer text state about {N} seconds later.
:later {N}m		Go to newer text state about {N} minutes later.
:later {N}h		Go to newer text state about {N} hours later.
:later {N}d		Go to newer text state about {N} days later.
g-			Go to older text state.  With a count repeat that many times. 
g+			Go to newer text state.  With a count repeat that many times.

For example, go back ten minutes with this command:
:earlier 10m
Sometimes you make several changes, and then discover you want to go back to when you have last written the file. You can do that with this command:
:earlier 1f
The “f” stands for “file” here. You can repeat this command to go further back in the past. Or use a count different from 1 to go back faster. If you go back too far, go forward again with:
:later 1f

Example about using undo branches in vim

Start a new file:
vim demo.txt
Press i to get into insert mode and type “Linux”:
Linux
Press esc. Now again enter insert mode and type “Unix”:
Linux Unix
Finally, press esc again and return insert mode by typing i and type “macOS”. My buffer will now contain
Linux Unix macOS

:echo changenr() command

To list the number of the most recent changes, type:
:echo changenr()
Sample outputs:

3

This will output three since you have made three changes. Remember we pressed the i three times to get into insert mode?. Next, I will press u to undo one change, start insert mode again and change the buffer so it will look like this:
Linux Unix FreeBSD
You have now branched from the previous version of your buffer and created a new undo branch:
:echo changenr()
Sample outputs:

4

If you press u in normal mode you will move back to
Linux Unix
A further undo using u will change the buffer to
Linux
And if you press u a final time, you’ll have an empty buffer again, as this is the state at which you started. You are now back before change 1 and
:echo changenr()
Sample outputs:

0

If you redo your changes using Ctrl-R, you will move to
Linux
then (press Ctrl-R)
Linux Unix
and finally (press Ctrl-R)
Linux Unix FreeBSD
Notice, that you never reached change word “macOS” in which the buffer contained ‘Linux Unix macOS’. You can however use the g- and :earlier commands to move back to that change. So if you now press g- or :earlier your buffer looks like this:

Linux Unix macOS

Of course if you know to which change you want to jump, you can also use :undo to jump to the specified change.
:undo 1
will put your buffer back at:
Linux

Plugins

The whole undo branching may sound complicated and confusing to use. Hence, I recommend the following plugins which are easy to use for advance undo and redo options:
1. mundo – Mundo is a plugin to make browsing this ridiculously powerful undo tree less painful:
vim mundo
2. undotree – The undo history visualizer for VIM. The plug-in visualizes undo history and makes it easier to browse and switch between different undo branches. Here is a quick demo:
Undotree demo
The undotree allows you to go back to a state when it is overwritten by a latest edit using vim branches feature. For most editors, if you make a change A, then B, then go back to A and make change C, normally you won’t be able to go back to B because undo history is linear. That’s not the case for Vim because it internally keeps all the edit history like a tree structure, and this plug-in exposes the tree to you so that you not only can switch back and forth but also can switch between branches.
See vim docs here for more info.

Conclusion

You learned how to undo something in Vim/Vi by pressing u. To redo the change, hit the Ctrl-R. We also covered advanced vim undo features, including plugins that will make your life easier.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at www.nixcraft.com

Historical Comment Archive

1 comment

    Still, have a question? Get help on our forum!