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
I find f and t very useful in combination with d and c. For example, ct:
will let you replace everything from your cursor up to the next colon, but not delete the colon. You can remember it as "change to colon".
fx
jumps to the next x
on the line.
tx
jumps to the character just before the next x
on the line.
You can use Fx
and Tx
to reach the previous x
.
You can use 2fx
to jump to the second x
on the line.
So, fF
and tT
are useful when you want to go quickly to the next set of parentheses (f(
) or delete everything from the cursor to, but excluding, the previous =
(dT=
) and so on…
See :h motion.txt. It will blow your mind.
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.
Just to add to Michael Kristofik's answer, no description of f
or t
is complete without also mentioning ;.
From this Vim cheat sheet:
;
"Repeat latest f, t, F or T [count] times."
So, to continue the @MichaelKristofik's theme:
The quick brown fox jumps over the lazy dog.
^
type fo
to go to the first 'o':
The quick brown fox jumps over the lazy dog.
^
and then ;
to go to the next one:
The quick brown fox jumps over the lazy dog.
^
Your first stop with questions like these should be vim's internal help, :h f
and :h t
. However, in this case, those entries are a bit cryptic without an example. Suppose we had this line (^
= cursor position):
The quick brown fox jumps over the lazy dog.
^
These commands find characters on a line. So fb
would place the cursor here:
The quick brown fox jumps over the lazy dog.
^
t
is like f
but places the cursor on the preceding character. So tb
would give you:
The quick brown fox jumps over the lazy dog.
^
You can remember these commands as f
ind and t
ill. Also, you can prepend the commands with a number to move to the nth occurrence of that character. For example, 3fb
would move to the third b to the right of the cursor. My example sentence only has one b though, so the cursor wouldn't move at all.