Introduction
Mastering the undo and redo commands in Vim is crucial for efficiently correcting mistakes and managing files. Vim's undo feature allows you to reverse unwanted changes with the u
command and restore them using Ctrl + r
.
This tutorial teaches how to undo and redo in Vim / Vi.
Prerequisites
- A Linux system.
- Access to a terminal.
- Vim installed.
Vim Change Tracking Explained
In Vim, change tracking allows you to keep track of edits made to a document, making it easier to review changes and undo them if necessary.
While Vim doesn't have a complex version control system, it provides basic functionality for tracking changes within an editing session through its undo and redo functions.
How to Undo Changes in Vim / Vi
Vim undoes changes by entries. An entry can be anything you do within one session in insert mode. Any changes made after pressing i
(to move into insert mode) and Esc
(to go back to normal mode) are considered one entry.
Also, an entry is a command you use after you press Esc
. This includes deleting lines and copying and pasting text in Vim.
The following text explains how to undo changes in Vim.
Undo Last Change
To undo changes made in the last Vim entry, use u
, :u
, or :undo
. For instance, the following text has five lines, each created in a separate entry.
To undo changes, switch to normal mode by pressing Esc
. Next, remove all changes made in the last entry with:
u
In this example, the last line in the text is removed.
Note: Use lowercase u
, not uppercase U
, which undoes all changes for one line. If you type U
by mistake, you can undo it with u
.
Undo Multiple Changes
To undo multiple changes, press Esc
to ensure you are in normal mode first. Next, specify the number of changes you want to undo in the file. To do that, add the number before the u
command:
[number]u
For instance, to undo the last three changes in the example file, run:
3u
Since each line was added as a single entity, 3u
undoes the last three lines in the text.
Undo Latest Changes in Line
Use the capitalized U
to undo all changes made within the current line since you started editing it, but only for that specific line. Unlike the regular undo command (u
), which undoes the most recent change anywhere in the file, U
restores the entire current line to its original state before any edits were made.
For example, the text below includes an edited version of line number five.
To undo all the changes done to the fifth line, press:
U
Redo Changes in Vim / Vi
In Vim, redoing changes allows you to reverse an undo operation, effectively restoring previously reverted edits. Use the Ctrl + r
command to redo the most recent undo.
For example, undo the changes made in the previous example with:
u
To reverse the undo operation, press Ctrl + r:
Redo Multiple Changes
To redo multiple undoes, use the following:
[number] Ctrl+r
For example, to restore three previous changes, press:
3 Ctrl +r
List and Access Undo Branches in Vim/Vi
In Vim, undo branches occur when you undo changes and then make a new edit, creating a separate path in the undo history.
Vim keeps track of these branches, allowing you to navigate and switch between them. To list and access these undo branches, use the :undolist
command, which shows a list of recent changes and their associated timestamps.
For instance, run:
:undolist
The output you receive should be similar to the following example:
It consists of four columns:
- Number. The change number.
- Changes. The number of changes made to the entity.
- When. The time when the change was made.
- Saved. Whether the file has been stored on disk and where.
You can then jump to a specific undo point by using the following:
:undo {number}
The number corresponds to a particular entry from the list. For example, jump to number 15 with:
:undo 15
To navigate through undo branches, use the :earlier
and :later
commands.
The :earlier
command allows you to move back to an earlier point in the undo history by specifying a time or number of changes.
For example, to go back to the change from five minutes ago, type:
:earlier 5m
Similarly, :later
moves forward in the undo history to redo changes after undoing them.
Once you have finished editing the file, make sure to save the Vim file before exiting.
Conclusion
This article explained how to use the undo and redo commands in Vim / Vi. Mastering these commands improves productivity and ensures you can easily recover from errors.
To learn even more about Vim, check out this cheat sheet for Vim commands.