153

I often navigate in vim by f x to find next occurrence of character 'x', but overlook that there is a word (or more words) containing 'x' in between the word I want to edit and the beginning cursor position.

So i have to f x again, which is kind of annoying since there is this nice button ., which does repeat the last command. So is there a way to repeat f x with a single button press.

epsilonhalbe
  • 15,637
  • 5
  • 46
  • 74

3 Answers3

296

The command to repeat an f is ; (semicolon); , (comma) reverses the direction of the search.

TechWisdom
  • 3,960
  • 4
  • 33
  • 40
Jeremiah Willcock
  • 30,161
  • 7
  • 76
  • 78
  • 6
    Is there a useful reason you can't use `.` for this? That was the command I expected to work. – Daniel Kaplan Mar 20 '15 at 22:58
  • 12
    @DanielKaplan It makes sense to map the novement, i.e. the search for a character (`f` and `;`), to a different character than the repetition of an edit (`.`). This allows you to do fast searches and changes to the search results. If you want to replace some `+` in a line with `*` you could do something like this (skipping some characters and replacing others): `f+r*;.;;.;.;;;.` – Marcus Krahl Jul 29 '15 at 13:29
  • 3
    Is there a way to remap those commands? A lot of people use `,` for leader. – rk1 Apr 23 '17 at 11:04
  • @rk1 Change the leader with :let mapleader=" " (if you want a space) –  Aug 24 '17 at 09:06
  • @DrEval Sure, but I want to keep my leader as `,` and still have reverse search. Anyway, now I'm a happy user of vim-easymotion. – rk1 Sep 12 '17 at 20:56
24

Time has passed since I asked this question - nowadays I use vim-easymotion, which makes the need for ; almost unnecessary.

This plugin allows to jump to a certain letter directly - triggering the plugin makes all letters grey except for all 'x' on the screen - and those are replaced by red letters which you can press to jump directly to it.

easymotion in use

epsilonhalbe
  • 15,637
  • 5
  • 46
  • 74
3

To add to @Jeremiah Willcock answer, We can add count to f command to go to the nth occurrence [count]f{char}. For e.g.

2fx => goes to the second occurrence of x [if available]

similarly we can use the same counter to ; and ,. For e.g.

2; => goes to the second occurrence of last search [if available]
2, => goes to the second occurrence of last search in reverse [if available]

This is very useful when using it with c{motion}(change) or d{motion}(delete). For e.g.
If we want to change or delete to 3rd occurrence of a char we can do

c3fx => change to 3rd occurrence of character x (included)
d3fx => delete to 3rd occurrence of character x (included)

Praveen
  • 8,945
  • 4
  • 31
  • 49