FUNDAMENTALS A Complete Guide for Beginners
To undo and redo changes in Vim, follow the below command:
- To undo the last change press
u
- To redo the last undone change press
Ctrl+r
- 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:This image shows the initial state of the file.
Now, omit the last line of that opened file: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: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:Now I will press
U
(uppercase u) from the normal mode to undo the most recent line modification: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: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:Here from my undo tree, I will undo the changes listed in number 10 (7 changes) by executing
:7u
: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:
- Firstly, the file is opened on the Vim editor:
- 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:
- At this stage, I will press u to undo the last change I made:
- Now, I will press
Ctrl+r
to redo the last change: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 org-
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
- How to Copy, Cut, and Paste Text in Vim? [All Cases]
- How to Search Text in Vim Editor? [8 Cases]
- How to Find and Replace Text in Vim? [5 Cases]
<< Go Back to Vim in Linux | Linux Text Editors | Learn Linux Basics