How to Undo in Vim / Vi text editor

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:
:undo N
:undo 3

To undo all latest changes on one line, run:

Undo changes in vim / Vi

  1. Press the Esc key to go back to the normal mode
  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.


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

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:


  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”:
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:


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:


If you press u in normal mode you will move back to
Linux Unix
A further undo using u will change the buffer to
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:


If you redo your changes using Ctrl-R, you will move to
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:


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:

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:

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.


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.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

Comments on this entry are closed.

Comments are closed. Still have questions? Post it on our forum