How to Undo and Redo Changes in Vim? [All Ways]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

To undo and redo changes in Vim, follow the below command:

  1. To undo the last change press u
  2. To redo the last undone change press Ctrl+r
  3. To undo several Vim edits, execute :<number of change>u

In this article, I will explore these approaches of how to undo and redo changes in Vim. So without further delay, let’s get started!

How to Undo in Vim / Vi?

To undo changes in Vim/ Vi, press the u key.

Now, let’s see the entire process through an example. First, open a file in Vim to edit:Initial state before undo and redo in Vim.This image shows the initial state of the file.

Now, omit the last line of that opened file:After editing the last line.The image shows that the last line has been omitted.

After that, switch to the normal mode by pressing ESC, and then press the u key from the normal mode to undo the omitting last line task:After undo.The image shows that the omitted last line has been restored.

Note: You can also execute :u, or :undo command from the normal mode to perform undo operation in Vim.

Undo the Most Recent Line Modifications

To undo the most recent line modification, press U(uppercase u) from the normal mode. In this example, I have a file opened in Vim editor:Initial state before undo and redo in Vim.Now I will press U (uppercase u) from the normal mode to undo the most recent line modification:Undo of the last change.The image shows that the most recent line modification has been reverted.

List of Undo Branches

To show the undo branches or tree, type :undolist and press the ENTER button from the normal mode. It eases to undo change and make new edits.

See in the following image:Undo tree or branch.The command has four columns:

  • Number: It displays the number of the changes.
  • Changes: It indicates how many modifications have been applied to the item.
  • When: It records the time of the edit.
  • Saved: It shows whether the file has been saved to the disk. In my case, no files are saved to the disk.

Undo Several Vim Edits

To undo several Vim edits, follow the syntax :<number of change>u.

Now for doing that, you can find the number of actions at the undo branches. At first, switch to the normal mode. Then, execute the :undolist to see the undo branches:Change number.Here from my undo tree, I will undo the changes listed in number 10 (7 changes) by executing :7u:Undo of several changes successful.The image shows that the undoing of several changes is successful.

How to Redo Changes in Vim / Vi?

To redo changes in Vim, press Ctrl+r.

Let’s demonstrate this by editing a file and then undo and redo the edit through commands. Join me as I go through the following steps:

  1. Firstly, the file is opened on the Vim editor:file to edit in Vim
  2. Now, I will switch to the insert mode and do some editing then switch to the normal mode again. After that, I will execute :w! to save the changes:file is being edited in Vim
  3. At this stage, I will press u to undo the last change I made:undoing changes made in Vim text editor
  4. Now, I will press Ctrl+r to redo the last change:redoing the change after the undo operation in Vim The image confirms the successful “redo” of the last modification.

Note: To redo several changes, press Ctrl+r as many times as you want to redo.

Advanced Tips and Tricks to Undo and Redo in Vim

Some advanced tips and tricks on undo/redo edits in Vim are listed below:

  • Press g+ to navigate forward and or g- to navigate backward through the time of the undo tree.
  • To go back x minutes, execute :earlier xm. Similarly, to undo the last x changes, execute :earlier x.
  • To move forward x minutes, execute :later xm. Similarly, to redo the last x undone changes, execute :later x.

Conclusion

In this article, I have demonstrated different aspects of undoing and redoing changes in Vim. I hope this article will be a helpful guide for Vim editor users. Don’t forget to put your suggestions in the comment box.

People Also Ask

How to redo with Vim?

To redo with Vim, press Ctrl+r. It will redo the last undone change in Vim.

How do I back in Vim?

To back in Vim, press u. It will undo the last change and move you back.

How do you discard edits in Vim?

To discard edits in Vim, type :q! and press the ENTER button.

How to save changes to the current file?

To save changes to the current file, type :wq and press the ENTER button.

How do I go back to the last edit in Vim?

To go back to the last edit in Vim, press g. It will use the changelist of the Vim and go to the last edit.

Related Articles


<< Go Back to Vim in Linux | Linux Text Editors | Learn Linux Basics

Rate this post
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment