Vim and Vi are among the most powerful text editors in the Linux world. They are lightweight, fast, and fully customizable, making them favorites among developers and system administrators. However, new users often find Vim’s command-based workflow confusing, especially when trying to undo or redo changes. This guide explains how to undo and redo edits in Vim and Vi step by step, using both command mode and visual mode.
1. Understanding Modes in Vim
Before learning the undo and redo commands, it is important to understand Vim’s modes. Vim operates in several modes, but the two most commonly used are:
- Normal Mode: Used for navigation, deleting, copying, pasting, undoing, and redoing text.
 - Insert Mode: Used for typing and editing content.
 
When you open a file in Vim, it starts in Normal Mode. Press i to enter Insert Mode and type your text. To return to Normal Mode, press the Esc key. All undo and redo commands work only in Normal Mode.
2. How to Undo Changes in Vim
Undoing changes is simple once you know the right command.
a. Basic Undo Command
To undo the most recent change, press:
uEach time you press u, Vim undoes the previous action. This could be deleting a line, inserting text, or making any other modification.
b. Undoing Multiple Changes
To undo multiple actions at once, type a number before pressing u.
For example:
3uThis command will undo the last three changes.
c. Undo All Changes in the Current Line
To undo all edits on the current line, use:
UNote that the capital U command resets the entire line to the state it was in before any changes were made during the current editing session.
3. How to Redo Changes in Vim
If you undo something by mistake, Vim allows you to redo it easily.
a. Basic Redo Command
To redo an undone action, press:
Ctrl + rEach time you press Ctrl + r, Vim reapplies the last undone change. You can use this repeatedly to redo multiple steps.
b. Redoing Multiple Changes
Like undo, you can also repeat redos with a number:
3Ctrl + rThis command redoes the last three undone changes.
4. Undo and Redo in Command-Line Mode
You can also undo and redo through Vim’s command-line mode. To enter it, press :
To undo, type:
:undoTo redo, type:
:redoThis method is especially useful when writing scripts or performing precise editing tasks.
5. Viewing and Navigating the Undo History
Vim maintains an internal undo tree that tracks your editing history. To explore it, use:
:undolistThis command displays a list of undo branches, each showing a timestamp and change number. You can move between undo states using:
:undo NReplace N with the desired change number.
6. Tips for Efficient Editing
- Save your file regularly using 
:w. - Use 
ucarefully to avoid removing more edits than intended. - Combine undo and redo with 
:undolistto move through complex changes. - Enable persistent undo so that your undo history remains available after closing Vim by adding this to your 
.vimrcfile: 
set undofileConclusion
Undo and redo are essential functions that make editing in Vim and Vi faster and more efficient. Once you master simple shortcuts like u for undo and Ctrl + r for redo, you can confidently navigate and correct your text edits without fear of losing work. Vim’s flexibility gives you complete control over your editing history, making it one of the most reliable tools for programmers and system administrators.
	





