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

Install Pip on Ubuntu 18.04: Python 3 and Python 2 Setup Guide

Pip is the official package manager for Python and the standard way to install libraries from…

3 days ago

Install R on Ubuntu 18.04 from CRAN: Statistical Computing Setup

R is an open-source programming language and environment built for statistical computing and data visualization. It…

3 days ago

Install Jenkins on Ubuntu 18.04: CI/CD Server Setup Guide

Jenkins is an open-source automation server that makes it easy to build CI/CD pipelines. Continuous integration…

3 days ago

Install Android Studio on Ubuntu 18.04 with Snap and OpenJDK 8

Android Studio is the official IDE for Android development, built on JetBrains' IntelliJ IDEA platform. It…

3 days ago

Install and Configure GitLab on Ubuntu 18.04 with Omnibus

GitLab is a web-based, open-source Git repository manager written in Ruby. It includes built-in tools for…

3 days ago

Install Anaconda on Ubuntu 18.04: Python Data Science Setup Guide

Anaconda is the most widely used Python distribution for data science and machine learning. It bundles…

4 days ago