Undo and Redo in Vim

To err is human. To undo is the error is super human.
Warp Terminal

Let me guess.

You made some mistakes while editing a file in Vim and now looking for a way to undo the previous action. Right?

Well, it is pretty easy to undo and redo in Vim and can be done by following the given keybindings:

Action Description
u Undo the last action.
Ctrl + r Redo the previous action.

Make sure you use these shortcuts in the normal mode (activated by pressing the Esc key).

Want a detailed explanation? I got you!

How to undo in Vim

To undo in Vim, all you have to do is follow the given steps:

  1. Press the Esc key to switch to the normal mode
  2. Press the u key to undo the last action (press it multiple times to undo earlier actions).

For example, here, I removed lines 3, 5, and 7 and now want to undo them so I will press the u key 3 times:

Undo in the vim editor

As you can see when I pressed the u key multiple times, it brought back all the deleted lines and when I pressed it again, it said Already at oldest change.

It simply means you brought the file to the previously saved position and if that's the new file and you keep pressing the u key, it will remove all the lines.

But if you don't want to keep pressing the u key, then you can specify how many times you want to perform undo at once by appending the number.

For example, if I want to undo the last 3 actions, then I will have to enter 3u (first, enter 3 and then u):

undo multiple actions in Vim
Icedrive - Next-Generation Cloud Storage - Get 10GB Free
Next-Generation cloud storage with Icedrive. Get started right away with a massive 10GB free space.

Partner Link

How to redo in Vim

For those who don't know, redo is used to revert the action done by undo. I mean you may have pressed the u key accidentally or you just changed your mind about the undo action itself.

In that case, simply follow these two steps:

  1. Switch to normal mode (if you haven't already)
  2. Press Ctrl + R to redo the action

For example, I used undo to get back lines that I deleted accidentally so now, I will use Ctrl + r 3 times to remove them again:

redo in vim

As you can see when I pressed Ctrl + r, it reverts the action done by undo and when I pressed it more than the required times, it said Already at newest change.

Which simply means there is nothing left to redo.

Similar to undo, you can also specify how many times you want to perform the redo action at once.

For that, you have to first enter the number of how many times you want to perform redo and then press Ctrl + r which will look like this: number + Ctrl + r.

Let's say I want to perform redo 3 times, so I will use 3 + Ctrl + r:

redo multiple times in Vim

Pretty convenient. Right?

Here's how to master Vim

If you want to master Vim and want to flaunt your skills, you have to start from scratch and learn all the basics first and for that, you can refer to our detailed guide on Vim commands:

Basic Vim Commands Every Linux User Must Know [With PDF Cheat Sheet]
A comprehensive guide explaining basic vim commands that will be useful to any Linux user be it a sysadmin or a developer.

Once you are done with the basics, here are some tips to get to the next level:

8 Vim Tips And Tricks That Will Make You A Pro User
Brief: In this article, I’ll show you some of my favorites Vim tricks with practical examples. If you don’t use Vim, these tips might not give reasons for using Vim but if you use it already, you’ll definitely become a better Vim user. Even if I recently

And if you want to take it even further, here's an excellent Vim course for you.

Mastering Vim Quickly

Master Vim like a true professional with this highly rated, premium Vim book and training course.

Chek it out

I hope you will find this guide helpful.

About the author
Sagar Sharma

Sagar Sharma

A software engineer who loves to write about his experience with Linux. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.

It's FOSS

Making You a Better Linux User

It's FOSS

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to It's FOSS.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.