MikeTheTall
MikeTheTall

Reputation: 3583

Vim: Repeat previous motion?

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).

Upvotes: 83

Views: 30352

Answers (6)

joeljpa
joeljpa

Reputation: 539

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

Case 1

For doing simple j/k jumps, nelstrom's answer[1] works well. To perform 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 besides just vertical jumps: say norm 10l for going right 10 steps.

Case 3

Macros are your friend. JK ABC's answer does say their usage "requires more spiritual power"[2] but once you learn the basics, you can wreak havoc on all repetitive tasks in your editor.

They can help us a little here. Look up the basics of macros if you aren't familiar with them.

  • 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 the advantage of being able to store any custom motion in different registers (a-Z, 0-9 etc). You can execute any one on the fly as you please and they will persist more or less.

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)

Upvotes: 8

Vivian De Smedt
Vivian De Smedt

Reputation: 1029

I have created a small vim-remotions plugin that allows to repeat the last motion using the ; and , keys (like for the f and t motions).

By default it repeats the following motions: ]m, ]M, ]], ][, }, g;, ]b, ]l, ]q, ]t, ]g.

It use the motion as they are defined in the buffer by the filetype.

The list of motions it repeats is configurable.

It can repeat the motion and their count or only the motion.

It can repeat the motion in the direction of the initial motion (like ; and , are doing for f and t) or in the direction of the document if considered more intuitive.

Thanks to @romainl for his hints.

Upvotes: 1

Santi
Santi

Reputation: 1812

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

,

Upvotes: 87

lenz
lenz

Reputation: 2425

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)

Upvotes: 2

Ingo Karkat
Ingo Karkat

Reputation: 172590

There are some plugins that provide this functionality:

Upvotes: 31

Hotschke
Hotschke

Reputation: 10190

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

Upvotes: 4

Related Questions