Vim: Repeat previous motion?

Vim

Vim Problem Overview


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

Vim Solutions


Solution 1 - Vim

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

,

Solution 2 - Vim

There are some plugins that provide this functionality:

Solution 3 - Vim

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

Solution 4 - Vim

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMikeTheTallView Question on Stackoverflow
Solution 1 - VimSantiView Answer on Stackoverflow
Solution 2 - VimIngo KarkatView Answer on Stackoverflow
Solution 3 - VimHotschkeView Answer on Stackoverflow
Solution 4 - VimlenzView Answer on Stackoverflow