Can somebody explain to me what the f and t commands do in vim and exactly how they work? I can\'t seem to find this information but people keep telling me
Since LondonRob mentioned ;
, I guess a description of the comma ,
command is in order. It is used very much in conjunction with these commands (when the search overshoots).
After performing a search with f
, F
, t
or T
, one could use ,
to repeat the search in the opposite direction.
Let's say we are at the start of this sentence, and we would like to change the elot to elit.
Lorem ipsum dolor sit amet consectetur adipiscing elot sed do eiusmod tempor.
^
I know I have to replace an o, so I perform an fo
(find o) immediately. The cursor is stuck at some early o in the line! Hit ;
to repeat the search in the same direction. Type type type... I should have just done it five times, but let's say I overshoot and type ;
six times instead. I end up here:
Lorem ipsum dolor sit amet consectetur adipiscing elot sed do eiusmod tempor.
^
Lorem ipsum dolor sit amet consectetur adipiscing elot sed do eiusmod tempor.
^
Lorem ipsum dolor sit amet consectetur adipiscing elot sed do eiusmod tempor.
^
Lorem ipsum dolor sit amet consectetur adipiscing elot sed do eiusmod tempor.
^
Lorem ipsum dolor sit amet consectetur adipiscing elot sed do eiusmod tempor.
^
Lorem ipsum dolor sit amet consectetur adipiscing elot sed do eiusmod tempor.
^
Lorem ipsum dolor sit amet consectetur adipiscing elot sed do eiusmod tempor.
^
Now, one could just perform a ,
twice to repeat this search in the other direction. The cursor will reach the o in elot.
Lorem ipsum dolor sit amet, consectetur adipiscing elot, sed do eiusmod tempor.
^
Lorem ipsum dolor sit amet, consectetur adipiscing elot, sed do eiusmod tempor.
^
ri
to finish the replacement.
As with most movement commands, ,
also take a count: [count],
.
From the manual:
Repeat latest f, t, F or T in opposite direction [count] times.