What do f and t commands do in vim and exactly how they work?
5 Answers
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.
-
40And if you write the letters in uppercase, then it jumps backward. So for example
Fb
orTb
.– user4923309Nov 11, 2016 at 23:56
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.
^
-
4the ; tip is pretty useful, though I don't see how it works with 't'. i tried and i am stuck in the same position no matter how many times the t command is repeated. Aug 13, 2016 at 19:33
-
1
-
@PeterPerháč, I guess it is trickier with the t command, since when searching forward for x, the semicolon will find the same x every time you hit ;, thus not moving. If you hit l once and then semicolon it should work.– mickeyMar 14, 2017 at 8:52
-
@PeterPerháč Make sure you aren't already at (one character before) the last occurrence on the line, and it should find the next occurrence. Sep 26, 2017 at 13:17
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".
-
10+1! Idioms like
ct,
orct)
to quickly replace one or last argument to a function call feel wonderful to use.– mike3996Sep 20, 2012 at 9:04 -
27I prefer
vt:c
because you don't spot always spot each occurrence of a given letter 100% correct. Withv
you can see the visual selection, a nice visual cue.– Bentley4Nov 27, 2013 at 19:00
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.
-
3
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.