How to Undo/Redo Changes in Vim/Vi

How to UndoRedo Changes in VimVi

Vim is a Unix-based text editor. It is an advanced and improved version of Vi text editor and translates to Vi Improved and is available on almost all Linux versions.

It is very common to mistakenly delete a line while working with the Vim editor or you might want to revert back the changes you made to an already existing line. Knowing how to undo and redo changes in Vim increases efficiency.

This tutorial will explain the difference between undo and redo commands and their practical uses. Vi and Vim both text editors support these commands.

Difference between :undo and x:redo Commands

Vim keeps track of changes made in the text in session entries. Any changes made between pressing the I key ( go into insert mode) and the Esc key ( go into normal mode) will be treated as a single session entry.

The :undo command will remove the changes in the order you made them. For example, if you press the I key and go into insert mode and write 5 lines and then press the Esc key to go back to normal mode. This will be treated as a single session and if you call the undo command, all 5 lines will be removed.

Any Linux command will also be treated as a single entry and the :undo command can also be used to revert changes by other Linux commands such as paste, delete, replace, insert, etc.

On the other hand :redo command is used to revert back any changes made using the :undo command.

Let’s take a deeper look at how to use these commands and their practical uses.

Undo changes in Vim/Vi

To undo any changes in the text using the :undo command follow the following steps:

  1. Press the Esc key to exit insert mode and enter normal mode.
  2. Press lower case u, :u, or :undo to call the undo command.
  3. Any changes made in the previous session entry will be removed.

For example, you have the following text in your Vim text editor and each sentence was added as a single entry. Now after pressing the Esc key to go into the normal mode you can call the undo command by typing in u or :u or :undo to remove the last sentence from your text.

knowing the redo and undo commands in vim can come in handy. You can easily revert back any changes you made in your text without much hassle. Undo command can easily revert back any changes you make.
~
~
:undo

When you press enter after typing in the :undo command the last sentence will be removed and a message will show saying 1 line less.

knowing the redo and undo commands in vim can come in handy. You can easily revert back any changes you made in your text without much hassle.
~
~
1 line less; before #9

Using the above describes commands you can undo any changes you made in a session. But the Vim :undo command can do much more.

Undo Multiple Changes

The Vim :undo command can be used to undo multiple changes with a single command. The command supports quantifiers, so it can be called with a number to undo the number of changes you want.

  1. Press the Esc key to enter normal mode.
  2. To specify the number of entries you want to revert you need to write the number with the undo command i.e :2u or :4u depending on the number of changes you want to revert.

Let’s say you want to revert the last 2 entries in the text mentioned below.

knowing the redo and undo commands in vim can come in handy. You can easily revert back any changes you made in your text without much hassle. Undo command can easily revert back any changes you make.
~
~
:2u

Now when you press enter the last two sentences will be removed from the text considering each sentence was added as a single entry. And the output will say there are 2 fewer lines.

knowing the redo and undo commands in vim can come in handy.
~
2 fewer lines

Undo Recent Changes in Line

The :undo command can also be used to undo the most recent changes on the current line you were working on.

  1. Press the Esc key to enter normal mode.
  2. Type capitalized U and hit enter or hold Caps Lock and U key. The last entry you made on the line most recent line will be removed.

Say we have written the following text and “practice makes perfect” was added as the last entry. You can type in the capitalized U command to remove that entry.

knowing the redo and undo commands in vim can come in handy. You can easily revert back any changes you made in your text without much hassle. Undo command can easily revert back any changes you make. Practice makes perfect. 
~
~
U

Now when you press the Enter key the last part of the text will be deleted.

knowing the redo and undo commands in vim can come in handy. You can easily revert back any changes you made in your text without much hassle. Undo command can easily revert back any changes you make.
~

If you accidentally press the capital U command you can call in the small :u command to undo the changes it made.

List changes made using the Undo command

To access a list of all the changes you have made in your text using the :undo command you can type in the following command:

:undolist

Now hit enter and a list containing information about the changes will be displayed.

Redo Changes in Vi/Vim

The :redo command is used to reverse back any changes you made using the :undo command. This command can be called by typing in :redo or by pressing Ctrl+R keys.

  1. Press the Esc key to exit insert mode and enter normal mode.
  2. Now either press Ctrl+R ( press and hold the Ctrl key and then press R key) or type in :redo and hit enter.

Let’s say you have already deleted the last sentence from the following text and now want to redo that change you made in it. Hit the Ctrl+R key or type in :redo command.

knowing the redo and undo commands in vim can come in handy. You can easily revert back any changes you made in your text without much hassle. Undo command can easily revert back any changes you make.
~
:redo

Now when you press the Enter key the last deleted sentence will be restored as shown below.

knowing the redo and undo commands in vim can come in handy. You can easily revert back any changes you made in your text without much hassle. Undo command can easily revert back any changes you make. Practice makes perfect. 
~

The sentence “practice makes perfect” has been restored.

Redo Multiple Changes

Follow these steps to redo multiple changes in Vim.

  1. Press the Esc key to enter normal mode.
  2. Now you need to type in the number of redos you want to do and then press Ctrl+R.

Each undo command can be reversed by a redo command.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)
0 Comments
Inline Feedbacks
View all comments
You May Also Like
Rm Command in Linux
Read More

Rm Command in Linux

The rm command is a very important command used to remove files and directories in Linux. The rm…