47

In Vim, with ., I can repeat the last normal mode command; for example:

dd.

deletes a line twice.
But, if I type

5j.

the cursor does't move 10 lines down. How do I repeat the last normal mode command, especially a move?

1
  • 3
    why don't you accept one of the answers below ? you forgot about it, or there's a reason ? May 3, 2020 at 13:43

5 Answers 5

50

it's doable even in vanilla vim, but applicability depends on your use case, ie. how often you'll need to repeat it, since it requires few more keystrokes to make it repeatable.


Option 1: turn it into a command mode operation

using moving down 5 lines as example, you can do:

  1. enter :norm 5j, it'll move the cursor down 5 lines
  2. use @: to repeat the movement

:norm stands for normal, any following string is regarded as your keystrokes under normal mode


Option 2: Use macro

  1. qa (store macro into register a, you can pick your own register like qb, qc)
  2. 5j
  3. q (finish recording macro)
  4. @a to repeat your recorded operation (replace a with the register name you picked, eg. @b, @c)

macro requires more spiritual power to set up but it's more repeatable in the sense that you can store multiple operations in different registers without being overriden by latest operations.

3
  • This should be the right answer!
    – look
    Feb 18, 2021 at 0:53
  • 5
    Option 2 all the way! Don't forget you can also re-run the last macro with @@. Also, I usually use the q register so the keyflow becomes qq5jq, @@. May 14, 2021 at 18:42
  • I've incorporated this and another answer, thus totalling it to 3 ways, over here for "Vim: Repeat previous motion?" at Stack Overflow. @JustinWrobel thanks for showing @@.
    – joeljpa
    Aug 25, 2023 at 5:50
15

vim doesn't do this unfortunately. The best you can do is install the repmo.vim plugin, which repeats movement commands that have a count.

1
  • I love repmo, but I've not got it to work with ggandor/leap.nvim. Do you have a solution to this, or a more recent alternative?
    – Tom Hale
    Nov 23, 2023 at 1:20
10

You can repeat changes with .. Movements can be repeated with the ; command. This command seems to be new and does a similar thing to the repmo.vim script mentioned in other answers.

For example, to move to the second next c, press 2fc. Then to do it again, just type ;.

3
  • 16
    ; isn't new. It's a standard vi command. Vi remembers the type and target character (but not numerical argument) of the last f/F/t/ T motion and ; repeats it. It doesn't repeat other motions, however.
    – pyrocrasty
    Jun 4, 2015 at 2:47
  • This accomplished what I was looking to do! thank you. and thank you @pyrocrasty for the clarification!
    – Paul
    Nov 30, 2018 at 18:55
  • 1
    @pyrocrasty Yes, showing that this is strictly limited to just those motions and can't do more needs to be shown in the answer.
    – joeljpa
    Aug 25, 2023 at 5:53
9

Actually . repeats the last change, not the last normal mode command. As Paul said, you will need a plugin to allow you to repeat motions.

See ":help .".

0

You can select the lines you want to change and execute last normal dot command

v5j .............. visual select next 5 lines
:'<,'>norm! .      normal mode execute over selection last command '.'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .