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

git fetch vs git pull: How They Work and When to Use Each

Both git fetch and git pull talk to a remote repository, but they do very different things to your…

5 days ago

git cherry-pick Command: Apply Commits from Another Branch

Sometimes the change you need already exists, just on the wrong branch. A hotfix lands…

5 days ago

Best Email APIs for Secure Business Email: Why Developers Are Moving Beyond SMTP

Email is still one of the most important communication channels inside modern applications. Password resets,…

6 days ago

Nginx Commands in Linux: Start, Stop, Reload, Test, and Log

Nginx is a high-performance web server and reverse proxy trusted by some of the largest…

1 week ago

ufw Command in Linux: Manage Firewall Rules with Examples

ufw (Uncomplicated Firewall) sits on top of iptables (or nftables on newer systems) and replaces…

1 week ago

who Command in Linux: Show All Logged-In Users and Sessions

When you share a server with a team or investigate unexpected activity, the first question…

1 week ago