How to Undo and Redo Changes in Vim and Vi

Try this guide with our instant dedicated server for as low as 40 Euros

redo vim

Ever wished you could bring back something you accidentally deleted in Vim or Vi? 

These text editors have a cool feature — undo and redo — that lets you easily correct the mistakes you make in the text. Whether you’re a beginner or a Vim pro, understanding how to undo and redo changes is necessary to utilize it appropriately.

In Vim’s command mode, the undo (CTRL-U) command erases recent changes effortlessly, while the redo (CTRL-R) command brings them back just as quickly. 

This quick tutorial will guide you through using these commands and handling undo branches for more complex edits. We’ll give you simple examples for undoing and redoing actions to help you understand these actions. 

overriding examples

Let’s start with the idea of undo in Vim/Vi.

Table of Contents

  1. Undo in Vim / Vi
    1. Undo the Most Recent Edits in Vim / Vi
    2. Undo Several Vim Edits
    3. Undo the Most Recent Line Modifications
  2. Redo Changes in Vim / Vi
    1. Redo Several Changes
  3. List Undo Branches
  4. Conclusion
  5. FAQs

Undo in Vim / Vi

Undoing changes in Vim or Vi is easy. 

In the Command mode, simply use the undo command by pressing the u key. Vim makes it simple to backtrack and keep your edits in check! 

Undo the Most Recent Edits in Vim / Vi

Let’s see the steps to undo the most recent edits in Vim/Vi:

Begin by entering the Normal mode by tapping the Esc key.

Next, use any of these commands to revert the edits from the most recent action:

u, :u, or :undo

All modifications and new information added in the previous entry will be deleted.

For instance, in the following document, every line has a distinct element. If you execute the: undo command, the most recent line added to the text will be deleted.

After pressing Enter, the most recent line is deleted.

overriding settings

Undo Several Vim Edits

You can extend the undo idea we mentioned earlier to multiple edits. For this, hit the Esc key to enter the Command mode. Next, to undo multiple changes, prefix the undo command with the number of actions you wish to reverse.  

In Vim, the syntax includes the number of changes you want to roll back before pressing u in this format:

[number]u.

Undo the Most Recent Line Modifications

A common use case of the undo action is to revert the most recent line changes. 

For this, enter the Command mode

 And revert the most recent single-line changes by entering the uppercase U for the undo command. 

Consider the following example file where we’ll use the undo command to the most recent line and explicitly removes the last sentence of the following input.

permitty

Here’s the output:

example of overriding settings

Redo Changes in Vim / Vi

Redoing changes in Vim / Vi is as easy as 1,2,3. 

  1. Ensure you are in Normal mode by pressing the Esc key.
  2. In Command mode, press Ctrl-R.
  3. Your changes will reappear without any delay!

Redo Several Changes

If you want to redo several lines/changes at once, follow these steps:

  • Begin by entering the Normal mode
  • To perform multiple redos simultaneously, press and hold Ctrl, then press r repeatedly for the number of redos you wish to execute.

List Undo Branches

When editing a file in Vim or Vi, it’s common to undo a change and make new edits. These undone changes aren’t lost; they become part of an undo tree—a collection of branches that you can easily navigate.

Listing undo branches in Vim is straightforward. Use the following command to view the branches of the undo history:

# :undolist

This command displays the undo branches, allowing you to navigate the changes made during an editing session. 

freedom house

Here’s a typical output of th command:

number of changes

As you can see, the command has four columns:

  • Number – This column displays the number of the change(s).
  • Changes – This indicates how many modifications have been applied to the item.
  • When – This column records the timestamp of the modification.
  • Saved – This shows if the file has been saved to the disk. If yes, this column will indicate its location.

Conclusion

Understanding how to undo and redo changes in Vim / Vi is essential for efficient text editing. With the Ctrl-R command, you can easily redo the last change or a series of changes. The undo Ctrl-U command in Vim helps reverse something you want back.

If you’re looking for a robust server for your projects, RedSwitches offers the best dedicated server pricing and delivers instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

Q. How do I undo changes in Vim?

You can use the “u” key in normal mode to undo changes in Vim. Pressing “u” will undo the last change you made.

Q. How do I redo the changes in Vim?

You can use the “ctrl+r” key combination in normal mode to redo the changes in Vim. Pressing “ctrl+r” will redo the last change you undid.

Q. How do I undo multiple changes in Vim?

Vim keeps track of all your changes and allows you to undo multiple changes. Press “u” multiple times to undo more than one change.

Q. How do I redo multiple changes in Vim?

Vim allows you to redo multiple changes. After undoing changes, you can press “ctrl+r” multiple times to redo the changes you undid.

Q. How can I see the undo tree in Vim?

Vim keeps track of the changes you make in an undo tree. You can use the command “:undolist” to see the entries in the undo tree.

Q. How do I go back to normal mode in Vim?

To go back to normal mode in Vim, you can press the “Esc” key. This will exit the insert or any other mode and return you to normal mode.

Q. What is the difference between Vi and Vim?

Vi and Vim are both text editors, but Vim is an improved version of Vi with additional features and enhancements.

Q. Can I undo and redo operations in Vi and Vim?

Vi and Vim allow you to undo and redo operations. You can use the “u” key to undo and “ctrl+r” to redo.

Q. How do I undo changes made in Vim using Git?

If you’re using Git, you can use the “git checkout — filename” command to discard changes and revert to the last committed version of the file.

Q. How do I redo the last change in Vim using Git?

If you want to redo the last change you made in Vim using Git, you can execute the “git diff” command to see the changes you made and then use the “git checkout — filename” command to revert to the changes you want to redo.

Try this guide with our instant dedicated server for as low as 40 Euros