vim repeat find next character 'x'

VimRepeat

Vim Problem Overview


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.

Vim Solutions


Solution 1 - Vim

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

Solution 2 - Vim

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

Solution 3 - Vim

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)

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
QuestionepsilonhalbeView Question on Stackoverflow
Solution 1 - VimJeremiah WillcockView Answer on Stackoverflow
Solution 2 - VimepsilonhalbeView Answer on Stackoverflow
Solution 3 - VimPraveenView Answer on Stackoverflow