Linux

How to Undo and Redo in Vim or Vi

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:

u

Each 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:

3u

This command will undo the last three changes.

c. Undo All Changes in the Current Line

To undo all edits on the current line, use:

U

Note 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 + r

Each 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 + r

This 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:

:undo

To redo, type:

:redo

This 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:

:undolist

This command displays a list of undo branches, each showing a timestamp and change number. You can move between undo states using:

:undo N

Replace N with the desired change number.

6. Tips for Efficient Editing

  • Save your file regularly using :w.
  • Use u carefully to avoid removing more edits than intended.
  • Combine undo and redo with :undolist to move through complex changes.
  • Enable persistent undo so that your undo history remains available after closing Vim by adding this to your .vimrc file:
set undofile

Conclusion

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.

0xSnow

0xSnow is a cybersecurity researcher with a focus on both offensive and defensive security. Working with ethical hacking, threat detection, Linux tools, and adversary simulation, 0xSnow explores vulnerabilities, attack chains, and mitigation strategies. Passionate about OSINT, malware analysis, and red/blue team tactics, 0xSnow shares detailed research, technical walkthroughs, and security tool insights to support the infosec community.

Recent Posts

Bash Scripting Best Practices Every Beginner Should Know

Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…

1 day ago

How To Create A Self-Signed SSL Certificate Using Bash And OpenSSL

Introduction A self-signed SSL certificate is a certificate that is created and signed by the…

1 day ago

How To Debug Bash Scripts Using bash -x And set Commands

Introduction Debugging is an important part of Bash scripting. When a script does not work…

1 day ago

How To Use Cron Jobs With Bash Scripts For Automation

Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…

1 day ago

How To Use Pipes In Bash Scripts For Command Chaining

Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…

1 day ago

How To Use grep, awk, And sed In Bash Scripts

Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…

1 day ago