Home
How to Use the Vim Redo Command and Navigate Undo Branches
In Vim, the redo command is the counterpart to the undo operation, allowing you to re-apply changes that were previously reversed. While most modern text editors treat undo and redo as a simple linear stack, Vim utilizes a sophisticated undo tree. This architecture ensures that no change is ever truly lost, even if you undo several steps and then perform a new edit.
The most direct way to redo a change in Vim is by pressing Ctrl + r while in Normal mode.
The Essential Redo Commands
Navigating back and forth through your editing history requires mastering a few key keybindings and Ex commands. These are performed in Normal mode. If you are currently typing in Insert mode, press Esc before attempting these commands.
The Standard Redo Shortcut
The primary method for redoing an action is the Ctrl + r shortcut. Each time you press this combination, Vim advances one step forward in the undo history, effectively "undoing the undo."
The Ex Redo Command
For users who prefer the command-line interface at the bottom of the editor, the :redo command (which can be abbreviated as :red) performs the exact same function as Ctrl + r. This is particularly useful if you are already in the command-line mode or are scripting certain editor behaviors.
Redoing Multiple Changes at Once
Efficiency in Vim often comes down to using counts. If you know exactly how many steps you want to redo, you can prefix the command with a number. For example:
5Ctrl + rredoes the last five undone changes instantly.:5redoachieves the same result via the command line.
Distinguishing Between Redo and Undo Operations
To use the redo command effectively, you must understand how it interacts with Vim’s different undo levels. Many beginners confuse the lowercase u and uppercase U commands, which affects how redo behaves afterward.
The u Command (Undo)
The lowercase u reverses the last single change. In Vim’s logic, a "change" is defined by the command used. A single dd (delete line) is one change. However, an entire session in Insert mode (from the moment you press i to the moment you press Esc) is also considered a single change.
The Ctrl + r Command (Redo)
This command specifically targets the actions reverted by u. If you haven't performed an undo, Ctrl + r will display a message stating "Already at newest change."
The U Command (Line Undo)
The uppercase U is a special command that reverts all changes made to the current line since you moved the cursor onto it. Crucially, Vim treats the U command itself as a new change. This means you can actually use the lowercase u to undo the effects of an uppercase U. Because of this circular relationship, the standard redo (Ctrl + r) does not directly interact with U in the same way it does with u.
Understanding the Vim Undo Tree
The real power of Vim’s redo system lies in its non-linear nature. In standard editors, if you undo five changes and then type something new, the "redo" history for those five changes is deleted. In Vim, those changes are moved to a new branch of the undo tree.
Why Linear Redo Fails
Imagine you write a function, then decide to delete it. You undo the deletion. Then, you decide to write a different version of that function. In a linear system, the first version you wrote is now gone forever. In Vim, the first version is stored in an alternate branch.
Navigating Branches with g- and g+
When you find yourself in a situation where Ctrl + r no longer works because you have started a new editing branch, you can use the "chronological" navigation commands:
g-: Move to an older state in the undo tree, regardless of branches.g+: Move to a newer state in the undo tree.
These commands walk through every state the file has ever been in during the current session, allowing you to recover text that would be considered "lost" in any other editor.
Time-Based Redo with the Later Command
Sometimes, it is easier to remember when you made a mistake rather than how many changes ago it happened. Vim provides powerful time-based navigation for redoing and undoing.
Using the :later Command
The :later command redoes changes based on a time interval. This is effectively a temporal redo. If you realize you liked the state of your code five minutes ago, but you have undone too far back, you can move forward in time.
Common syntax for :later:
:later 5s: Redo changes made in the last 5 seconds.:later 10m: Move the buffer state forward by 10 minutes.:later 1h: Move forward by one hour.
Using the :earlier Command
Conversely, :earlier moves the state backward in time. Combined, these commands allow you to scrub through your editing session like a video timeline. In my experience, this is incredibly useful when refactoring large blocks of code where you might have lost track of individual u and Ctrl + r counts.
Managing Undo Blocks for Better Redo Granularity
One common frustration with the redo command is the "all or nothing" nature of Insert mode. If you spend twenty minutes writing a long paragraph without leaving Insert mode, a single undo will delete the whole paragraph, and a single redo will bring it all back.
Breaking Undo Blocks with Ctrl-g u
You can manually create "save points" or breaks in your undo history without leaving Insert mode. By pressing Ctrl-g u, you tell Vim to end the current undo block and start a new one.
I frequently use this when writing documentation. For every new sentence or logical thought, I type Ctrl-g u. This ensures that if I need to redo a specific sentence, the Ctrl + r command targets only that segment rather than the entire page of text.
Joining Undo Blocks
On the other side of the spectrum, the :undojoin command allows you to merge the next change into the previous undo block. This is generally used in scripting and automation to prevent a series of automated edits from cluttering the undo/redo history.
Enabling Persistent Undo
By default, Vim clears the undo and redo history as soon as you close a buffer or exit the editor. This can be a major disadvantage compared to modern IDEs. However, Vim supports "Persistent Undo," which saves the entire undo tree to a file on your hard drive.
Configuring VIMRC for Persistence
To ensure that you can use the redo command even after restarting your computer, you need to add specific lines to your .vimrc file.