77

Let's say that I'm in visual mode, and I type "aw" to extend the visual area to include the next word. I'd like to then include the next couple of words. Is there a single key that I can press to repeat the previous motion (include text object motions)?

I'm aware that '.' repeats the previous change, and 'n' repeats the previous search, amongst other 'repeat' commands, but I'm unaware of any command to repeat the previous motion (whatever it was).

MikeTheTall
  • 3,214
  • 4
  • 31
  • 40
  • 19
    Builtin repeat motion commands `,` and `;` only work for motions `fFtT`. No general command, you have to use a plugin. – Hotschke Feb 13 '15 at 08:04
  • 1
    There are a few things you can still do without plugins. For instance, see https://superuser.com/questions/429917/repeat-last-normal-mode-command-including-moves-in-vim I've consolidated whatever I found in [my answer.](https://stackoverflow.com/a/76778797/) – joeljpa Jul 27 '23 at 10:29

5 Answers5

78

Well there is a command to repeat every motion made with f,t,F or T Remember that
fx takes to the next occurrence of the character x
Fx takes to the previous ocurrence of x
tx takes you to the character before the next occurrence of x
Tx takes you you to the character after the previous occurrence of x
to repeat those motions press

;

to repeat them backwards (in the oposite direction) press

,
Santi
  • 1,682
  • 1
  • 13
  • 14
  • 5
    This is by far the best answer. – Caveman Feb 09 '19 at 15:52
  • 8
    Of note, when repeating `t` or `T`, `;` is different than just typing the command again. For example, if you type `t(t(`, you won't get past the first `(` character. But if you type `t(;`, you'll go to the second `(`. See `:help cpo-;`. – Andrew Keeton Nov 15 '19 at 18:52
  • 1
    There seems to be no built in way to repeat motions like `gE`, `10j`, ... – Nikhil CSB Oct 10 '22 at 14:40
  • 1
    @NikhilCSB There are still some things you can do. See [my answer](https://stackoverflow.com/a/76778797/). – joeljpa Jul 27 '23 at 10:27
30

There are some plugins that provide this functionality:

Hotschke
  • 9,402
  • 6
  • 46
  • 53
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • 4
    I wanted to repeat `[{` style motions. `repeatable-motions.vim` works out of the box for that (repmo doesn't appear to). – dsummersl Dec 03 '15 at 15:09
  • 3
    It exists in emacs. Get on it, Vim devs! https://stackoverflow.com/questions/275842/is-there-a-repeat-last-command-in-emacs – Nathan majicvr.com Mar 22 '18 at 16:35
3

Instead of repeating the motion, there is a plugin for expanding regions via + and shrinking _: https://github.com/terryma/vim-expand-region

Hotschke
  • 9,402
  • 6
  • 46
  • 53
2

NO PLUGINS use ;.

Here's a detailed example:

int function1(){
   some code here;
   ...
   ...
   return 0;
}

int function2(){
   some code here;
   ...
   ...
   return 0;
}

Now let's say you want to rewrite the part inside {} for function1 and function2.

  • Place your cursor somewhere inside the {}
  • From Normal Mode (ESC or ctrl+c), press di} (for "delete" "inner" "{")
  • Now you've deleted everything in the function
  • Now bring your cursor inside the {} of function2
  • Press ;.

For visual mode, I think macros is your only option (maybe overkill for aw)

  • To start recording a macro, press q + q (that last q can be any letter. It's where you macro will be saved)
  • ... do the actions you want to repeat ...
  • Press q again to stop recording

To replay these actions (even in visual mode):

  • @q (or whatever letter you saved it to)

To replay the actions 99 times:

  • 99@q (or whatever letter you saved it to)
lenz
  • 2,193
  • 17
  • 31
  • 3
    Feeling a bit shocked that I'm only now learning about the `di}` movement... – Eidolon108 Mar 14 '22 at 18:05
  • 2
    wouldn't it work if you just pressed `.` no `;`? – Jake May 22 '22 at 23:28
  • While this is interesting and I did learn something, I don't see how this answers the question. OP asked for ways to repeat a previous motion. This deletes text under a function and redoes that again? Do correct me if I'm wrong. – joeljpa Jul 25 '23 at 07:20
0

There's still some hope if you're aiming to go sans plugins.

Case 1

For simply doing j/k jumps, nelstrom's answer (1) works well. For performing 100j and 100k respectively:

  • :+100 or :-100
  • @: (repeats the last command).

Case 2

Using norm. From the manual:

Execute Normal mode commands {commands}. This makes it possible to execute Normal mode commands typed on the command-line.

  • norm 100j followed by our trusty @:

The advantage here is we can apply this to do other motions too, say norm 10l for going right 10 steps.

Case 3

Macros are your friend. JK ABC does say "requires more spiritual power"(2) and once you learn the basics of using them, you can wreak havoc on all repetitive tasks in your editor.

They shine when it comes to repeating motions too. Look up the basics of macros if you aren't familiar with them already.

  • q1 (start recording a macro in register 1)
  • 100j (your movement)
  • q (stop the recording)
  • @1 (replay macro at register 1. If done once, you can then use the easier-on-the-hands @@ to re-run the last used macro)

This has even more flexibility in the sense that you can store any custom motion in different registers (a-Z, 0-9 etc) and execute any one at a flick of your wrist.

References

  1. nelstrom's answer from How to jump down X amount of lines, over and over
  2. JK ABC's answer from Repeat last normal mode command, including moves, in Vim (Super User)
joeljpa
  • 317
  • 2
  • 13