undo & redo in vim editor

How to Undo & Redo in Vim / Vi Editor

Vim/Vi Editor is a popular text processor used by document editors, software developers and system administrators all over the world. Sometimes while using Vim/Vi Editor we may end up making mistakes or deleting stuff that was important. Then we may want to undo our past action in Vim/Vi editor. Sometimes you may want to repeat your past tasks performed in vi editor many times. It may be tedious to do this and you may want to simply redo changes in Vim/vi editor. In this article, we will learn how to undo & redo in Vim/Vi editor.

How to Undo & Redo in Vim / Vi Editor

Vim/Vi editor is installed on almost every Linux & Mac system. It keeps track of all changes you make in the current session, or until you exit vi editor. Here are the steps to undo & redo changes in it.

Undo in Vim/Vi Editor

You can easily undo changes using u, :u or :undo commands.

First press Esc key to to go to normal mode. Enter u key to undo last change. You can prefix a number to this command to undo multiple changes. For example, you can enter 3u to undo last 3 changes. Please note, you need to use the lowercase ‘u’ and not the uppercase ‘U’.

You can use undo command to reverse changes made by paste, search & replace, delete, etc. Please note, when you are in insert mode, all changes that you make to your text are considered as a single change. So if you go to insert mode, make changes at 5 different places in your text, go back to normal mode and press ‘u’ all changes will be reversed at once.

Redo Changes in Vim/Vi Editor

Sometimes you may want to repeat the last change multiple times, like pasting a text multiple times. Doing it manually is very tedious. So it is easier to simply execute the redo command in Vim/Vi editor.

To redo a change in Vim/Vi editor use Ctrl+R or :redo. Press Esc key and go to normal mode. Enter Ctrl+R to redo last change. If you enter Ctrl+R again, Vim editor will again redo the last change. You can also use qualifiers, for example, enter 4Ctrl+R to redo last 4 changes.

Please note, every undo command can be reversed using redo command, and vice versa.

In this article, we have learnt how to undo & redo in Vim. These are two very useful but lesser known commands in Vim/VI editor often required by almost every user. They make it easy to edit documents and save time.

Also read:

How to Set Timeout in cURL
How to Set Indentation in Vim
How to Show Progress in Rsync
How to Clean up Snap Packages
How to Fix Unacceptable TLS Certificate Error

Leave a Reply

Your email address will not be published. Required fields are marked *