Skip to content

How to undo/redo in Vim?

  • by
  • 3 min read

Vim is a rather popular command-line text editor which is preinstalled on a lot of Linux distros and is used by a lot of Linux based developers and admins.

The editor is simple to use, works with pretty much all file formats and gives you essential features such as find, cut, copy, paste, and undo and redo.

In this article, we’re taking a look at how to undo or redo in Vim.

Also read: How to access command line history on Linux?


Undoing changes in Vim

Since Vim keeps track of all changes you’ve made in an active session, you can undo your changes by using the u, :u or the :undo commands. Just follow these steps.

Step 1: If you’re writing in Vim, make sure to press the Esc key to go back to command (normal) mode. 

How to undo/redo in Vim?

Step 2: Type the u command to undo the last change once you’re in command mode.

How to undo/redo in Vim?

Since the undo command in Vim accepts quantifiers, you can pass a number along with the command to tell Vim to undo a certain number of changes. For example, typing 5u will undo the last five changes you made.

Another thing to keep in mind is the difference between and U. The uppercase command undoes all the latest changes on one line compared to the lowercase command, which undoes changes across the document. 

Also, keep in mind that when working in insert mode, all changes to the text are considered one entry in the undo tree. This means that if you add 10 lines in insert mode and then switch to command mode and use the u command, all 10 lines will be removed. 


Redoing changes in Vim

Redoing changes in Vim is even easier than undoing. Just follow these steps.

Step 1: Make sure you’re in command mode. Press Esc if you’re in insert mode to switch to command mode.

How to undo/redo in Vim?

Step 2: Hold down the Ctrl key and press to redo the change. Since the command accepts quantifiers, you can specify the number of changes you want to redo by adding a number before the command. 

How to undo/redo in Vim?

Also read: How to fix ‘Sudo command not found’ error on Linux?

nv-author-image

Yadullah Abidi

Someone who writes/edits/shoots/hosts all things tech and when he's not, streams himself racing virtual cars. You can contact him here: [email protected]

>