Mastering Vim: Undo, redo and repeat

This time I’ll show you how to undo, redo and repeat stuff in Vim.
With just a few simple keystrokes you can improve your daily workflow.

Undo

Undo things in Vim is quite easy, as you can just press the u key in normal command mode.

The u key will navigate through the history of your changes. This means everytime you press it, one more change will be undone. You can also use a quantifier, such as 3u (undo the last 3 changes).

Please note that the u is lowercase. If you use an uppercase U instead, the whole last line you’ve modified will be reverted to its original state. However, U will not navigate through the undo history, but instead of it create a new undo history entry.

Redo & simple repeating

When we talk about “redo” things, we’ve to differ between the following two key strokes in normal command mode:

  • . will repeat the last change you’ve done
  • Ctrl-r will redo (revert) a previous undone change

Here are some examples for both commands.

Let’s say you’re switching to insert mode (i), press the comma (,) key and then go back to normal mode (ESC). You just inserted a comma under your cursor – that’s your change! Now you go a line down and want to do the same thing. At this point, you can simply press the . key, and the last change (insert a comma) is repeated.

Now switch to a different example where you did some changes you want to undo. So you press u several times, until you realize you did one undo too much. What you want to do now is redo your undo change, and this is where you’d use Ctrl-r.

Macros

We already know the repeat (aka .) command, our useful little helper. But what about more complicated scenarios, where you want to repeat a series of commands instead of only one? Well, this is where Vim macros come in place.

Recording macros

A macro is simply a sequence of commands. Macros can be recorded by using the following steps:

  1. First press the q key (macro key)
  2. Choose the macro register by pressing one of the a-z keys
  3. Enter your (complicated) Vim commands you want to record
  4. Finally press the q key again

Now you’ve recorded your series of commands in a macro and stored it in a register. Whenever you start recording a macro, you should see something like this on the bottom margin of Vim:

recording @q

Let’s say I want to do this:

  • Add the letter X right before the last character of the current line
  • Go to the next line
  • Jump to the first character

I’d use the following key combination:

qq$iX<ESC>j^q
  • qq is starting the macro recording on register q
  • $ is jumping to the last character in line
  • i is switching to insert mode
  • X is the letter X
  • <ESC> is switching back to normal mode
  • j (or alternatively down cursor) is jumping to the next line
  • ^ is jumping to the first character in line
  • q is stopping the macro recording

Playing macros

Of course a macro is only worth something if you can play / repeat it. This is where the @ key comes into place.

To play your macro simply press the @ key, followed by the register you’ve chosen before. In the example above, you can see my register was q, so I simply repeat it by pressing @q. You can also specify a qunatifier, such as 3@q (repeat macro q 3 times).

Appending to macros

If you want to append something to an existing macro, simply use the uppercase letter of your register. With that in mind:

  • qx…q will replace the macro on register x
  • qX…q will append to the macro on register x

Displaying all registers

To display all existing registers, simply use the following command-line command:

:reg[isters]

You can also display a single register by specifying it after the :reg[isters] command.

More about macros

More about macros, such as editing & saving them can be found on this wiki page.

61 Comments

  • impossible game

    With just a few simple keystrokes you can improve your daily workflow.

  • jenny

    Thanka a lot

  • minion rush

    You have remarked very interesting points!

  • Daikichi

    Thanks for the useful info

  • clash royale pc

    Vim is a clone, with additions, of Bill Joy’s vi text editor program for Unix. Vim’s author, Bram Moolenaar, based it upon the source code for a port of the Stevie editor to the Amiga and released a version to the public in 1991.

    The best way to learn is to practice. Take a few minutes to try Vim out. If you’re on a Linux system right now, open up a terminal and type vim filename. Enter insert mode and type a bit (or copy some of the text from this article into Vim) and then hit Escape to start practicing movement around the file.

  • jobi johnson

    Nice post. Thank you to provide us this useful information.

  • Susan Thompson

    Sufficiently, the article is really the best theme on this vault related issue. I fit in with your decisions and will vigorously predict your next updates. Endeavoring to offer significant thanks won’t simply be agreeable, for the fantasti c clarity in your creation. I will promptly get your rss channel to remain educated as for any updates.

  • neuropathy chiropractor

    And indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.

  • Succeed! It could be one of the most useful blogs we have ever come across on the subject. Excellent info! I’m also an expert in this topic so I can understand your effort very well. Thanks for the huge help.

  • slope unblocked

    What happens to you, please don’t happen to me
    I broke up but I won’t … What to regret.

  • deeeepio

    Maybe the two of us will end up sad soon?
    This pain I gave to the wind.

  • run 3

    Baby, take a chance
    ‘Cause I want this to be something
    Straight out of a Hollywood movie
    I see you standing there
    And all I can think

  • landandnest.com

    All I can say is Vim is good for starters.

  • Taylor Barber

    Agree! Vim is such a good app.

  • contact us

    Any tutorial videos for Vim available here?

  • John

    Great tips I learned from this. Thanks for sharing us your knowledge.

  • click here

    Mastering these steps will surely help us.

  • The Justice Project

    These are great step! Thanks for sharing

  • style guide

    I’ve been looking for these steps on the internet. Finally!

  • Building Signs

    Vim is very useful for these macros.

  • cherry u

    One really needs to master these steps! This is very helpful! Thanks for sharing!

  • construction seta

    thanks for these tips about Mastering Vim, keep it up.

  • Plumber Pretoria

    great article, have a lot of information you gain from this article, keep it up.

  • plumber

    thanks for these great tips. appreciated much.

  • Aylesbury Plumber

    im grateful to browse your site. this is a big help to me. keep posting

  • Plumber near me

    A great write-up is easy to understand what they mean. thanks for the instruction on how to Redo, Undo and Repeat. Keep posting

  • plumbers

    Thanks for these grateful instructions, it’s a big help for me. keep posting.

  • Plumber Eastbourne

    highly appreciate your effort to share this with us.

  • Rochdale Plumbers

    simple instruction but most important. thanks

  • Putney Plumber

    im pretty sure, there’s so many viewers happy on this site because have a lot of knowledge that they gain.

  • Plumbers near me

    already follow your instructions, it works.

  • www.lakewoodconcrete.org

    I’m still new to VIM and this information you shared is extremely helpful. Thank you!

  • CedarParkFencing

    Our educator didn’t show us this technique. I’m glad that you did. Thanks

  • .Book Review Writing Help

    Keep up the wonderful piece of work, I read few posts on this internet site and I think that your blog is really interesting and holds bands of fantastic information

  • kitchen remodeling

    You are just great! Wow! Thank you for this! It helped me a lot.

  • plumbers near me

    thanks for the codes. it such a big help.

  • polokwane plumber

    keep doing great information you shared with us.

  • sattamatka

    It was a very good post indeed. I thoroughly enjoyed reading it in my lunch time. Will surely come and visit this blog more often. Thanks for sharing.

  • siouxfallstrees.com

    I’ve been using Vim for a few years and still learned a bunch of stuff from this

  • Kieth

    Very cool

  • timber floor sanding newcastle

    Like any editor, Vim keeps track of every operation. Press u to undo a last operation, and Ctrl + r to redo it.

  • Hard Wood Floor Refinisher

    The best way to learn is to practice. Take a few minutes to try Vim out. If you’re on a Linux system right now, open up a terminal and type vim filename. Enter insert mode and type a bit (or copy some of the text from this article into Vim) and then hit Escape to start practicing movement around the file.

  • https://pergolaswollongong.com/

    I’m impressed, I have to admit. Really rarely do you encounter a weblog that’s both educative and entertaining. Thanks a lot!

  • Knight Shift

    Thank you for sharing this amazing post, great information. Keep sharing.

  • link olx

    Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place..

  • assignment provider

    We provide assignment help from K-12 to the college level and are believed assignment help suppliers for the student local area around the world. In the event that you need help in writing assignments, we will coordinate with you to a specialist, who can give you direction consistently.