Oct 10, 2023 3 min read

How to Undo and Redo in Vim / Vi

Learn How to Undo and Redo in Vim with our step-by-step tutorial. Vim is a text-editor for Unix that comes with Linux, BSD, macOS etc.

Undo and Redo in Vim/Vi
Table of Contents

Introduction

Before we begin talking about how to undo/redo in Vim. Let’s briefly understand - What is Vim?

Vim is a text-editor for Unix that comes with Linux, BSD and macOS and is known to be fast and powerful as it is a small program that runs in a terminal.

When editing a file, you can accidentally erase a line or change your mind about an alteration, and you'll want to undo your changes.

On macOS and practically all Linux distributions, Vi or Vim is installed by default. Knowing the fundamentals of Vim could come in handy if your preferred editor is unavailable.

This tutorial will show you how to undo and redo changes in Vim. We will also address a few FAQs on how to undo and redo changes in Vim.

Undo Changes in Vim / Vi

Vim maintains track of all the modifications you've made so far in the session. The undo command reverses the sequence in which one or more modifications were done.

Use the u, :u, or :undo commands in Vim and Vi to undo changes:

  1. Press the Esc key to return to normal mode, commonly known as command mode, whether you're in insert or any other mode.
  2. To undo the last change, type u. The u command in Vim also takes quantifiers. 4u, for example, might be used to undo the four most recent changes.

Make sure to write lowercase u rather than uppercase U, which will reverse all recent changes on a single line. If you enter U by mistake, you can undo the change with u.

Use the undo command to reverse the effects of any other action, such as delete, paste, search and replace, and so on. All text changes in insert mode are considered as a single entry in the undo tree.

If you enter insert mode and type five lines, then return to regular mode and press u to reverse the change, you will remove all five lines.

Redo Changes in Vim / Vi

The undo feature is reversed by the redo feature.

To undo a change in Vim or Vi, press Ctrl-R or :redo :

  1. Press the Esc key to return to normal mode.
  2. To undo the most recent change, press and hold Ctrl while pressing R. Quantifiers can also be used in Vim. If you wish to undo the last four changes, for example, use 4Ctrl-R.

A redo command can be used to reverse each undo command.

FAQs to Undo and Redo lines in Vim

How can I undo the most recent change in Vim/Vi?

Press the u key in command mode to undo the most recent change.

Are there any other ways to undo changes in Vim/Vi?

Yes, you can use the command :undo followed by the number of changes you want to undo.

What is the maximum number of changes that can be undone in Vim/Vi?

The number of changes that can be undone in Vim/Vi depends on the undolevels setting in your Vim configuration, typically set to a default value of 1000.

Can I undo multiple consecutive changes at once in Vim/Vi?

Yes, you can use the command :undo <number> to undo multiple consecutive changes in one go, replacing <number> with the desired value.

How can I redo the most recent undo in Vim/Vi?

Press Ctrl + r in command mode to redo the most recent undo.

Is there a limit to the number of changes that can be redone in Vim/Vi?

There is no explicit limit on the number of changes that can be redone in Vim/Vi. It is generally determined by the number of undo levels available.

Can I undo changes made in a different file in Vim/Vi?

No, undo and redo actions are limited to changes made within the current file open in Vim/Vi.

How can I redo a specific undo action from the undo history in Vim/Vi?

You can find the specific undo action in the undo history displayed by :undolist and redo it using :redo followed by the appropriate undo number.

Conclusion

In Vim/Vi, type u to undo a change and the Ctrl-R key sequence to redo a change that was undone.

The tutorial will act as a fundamental skill for every Vim/Vi user.

Undo branches are also supported in Vim.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.