vim

How to undo and redo commands in VIM

Vim is one of the most famous and powerful text editors. It is highly efficient and empowers users to manipulate files with few keystrokes. The thing that makes VIM unique is that it is among one of those utilities that are both simple and powerful.

The minimalistic interface of Vim makes it a simple text editor. It helps you to stay focused on your primary task i.e. writing. Similarly accomplishing a complex task easily, quickly and smartly with Vim makes it a powerful text editor. In short, Vim comes with a lot of useful features that allow you to accomplish any task with minimal effort.

In this article, we will talk about one of the most useful features of vim that is undoing and redoing the changes done in text files.

Undo/redo in Vim

Suppose you are editing a file and made couple of changes but then you realize you have done something wrong and you want to remove the changes. This is where undo comes. It allows you to revert to the state where you have been earlier. Also, you can redo the changes that were made previously. Note that changing the file means changes in the text, not the pointer movements and some other activities that are not associated with the text.

How it works

Vim maintains a record of changes that we have made in an undo stack in the form of “last in first out”. In Insert mode, when we add or remove some text, this action is added in the undo stack as one item. When you perform undo, this item is removed from the undo stack and added to the redo stack.

Undo Changes

Let’s suppose you have done some changes in a file like adding or removing the text. Then you have realized that you have done wrong and want to undo the changes.

To undo the changes in Vim editor, press “u”.

Understand the below points to undo the changes that you have made:

  1. To undo the changes, make sure you are in Normal mode as this command works only in Normal mode (also known as command mode). As you might know, the Vim editor has two modes: Normal mode and Insert mode. If you are in Insert mode, press Esc to shift to Normal mode.
  2. Then press the “u” key once and your last change will be undone. Now if you are satisfied with it, save the file otherwise keep pressing “u” to undo the changes one by one until you see the “Already at oldest change” message at the bottom left of the Vim Editor. It shows that the file is now in the initial state when it was opened.
  3. You can also use numbers with undo command like if you want to undo the last three changes, type 3u.
  4. All the changes done in one instance of Insert mode will be considered as one change. For instance, in Insert mode, you have made 3 changes and then shifted to normal mode. Now if you press “u” once, all the three changes will be undone.

Now take another example to make it clear. If you have made three changes in Insert mode and then shifted to normal mode. Then again. Let’s say, you enter in Insert mode and made one change. In this case, if you press “u” once, only the last one change will be undone.

Tip: If you have not saved the file after making the changes, then simply type:quit! and press Enter in Normal mode. It will quickly undo all your changes that were made after the last time you saved the file.

Redo changes

To redo the changes that were undone by the undo command, use Ctrl+r. It will bring back the last change you have made.

Understand the below points to undo the changes that you have made:

  1. Similar to undo, the Redo command “Ctrl+r” also works in Normal mode. If you are in Insert mode, use the Esc key to shift to normal mode.
  2. Now, to redo the last change, press “Ctrl+r”. If you are okay with it, save the file otherwise keep using “Ctrl+r” to redo remaining changes until you see the “Already at newest change” message at the bottom left of the Vim Editor.
  3. You can also use numbers with redo command like if you want to redo the last three changes, use 3Ctrl+r.

This is how the undo and redo works in Vim. The process is quite simple, press U to undo a change, and press Ctrl+r to redo it again. I hope you like the article.

About the author

Karim Buzdar

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn.