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.

41 Comments

  • impossible game Reply

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

  • jenny Reply

    Thanka a lot

  • minion rush Reply

    You have remarked very interesting points!

  • clash royale pc Reply

    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 Reply

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

  • Susan Thompson Reply

    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 Reply

    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 Reply

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

  • deeeepio Reply

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

  • run 3 Reply

    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

  • contact us Reply

    Any tutorial videos for Vim available here?

  • John Reply

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

  • click here Reply

    Mastering these steps will surely help us.

  • The Justice Project Reply

    These are great step! Thanks for sharing

  • style guide Reply

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

  • cherry u Reply

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

  • Plumber Pretoria Reply

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

  • plumber Reply

    thanks for these great tips. appreciated much.

  • Aylesbury Plumber Reply

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

  • Plumber near me Reply

    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 Reply

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

  • Putney Plumber Reply

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

  • CedarParkFencing Reply

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

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.