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.
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:
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.
Undoing changes is simple once you know the right 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.
To undo multiple actions at once, type a number before pressing u.
For example:
3uThis command will undo the last three changes.
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.
If you undo something by mistake, Vim allows you to redo it easily.
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.
Like undo, you can also repeat redos with a number:
3Ctrl + rThis command redoes the last three undone changes.
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.
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.
:w.u carefully to avoid removing more edits than intended.:undolist to move through complex changes..vimrc file:set undofileUndo 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.
Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…
Introduction A self-signed SSL certificate is a certificate that is created and signed by the…
Introduction Debugging is an important part of Bash scripting. When a script does not work…
Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…
Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…
Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…