Character Surfing: Understanding and Remembering the `f` and `t` Commands In Vim
Thu Oct 19 2017 | Basics | 0 CommentsIt took me years to start using two of the most helpful Vim navigation commands available.
My excuse for not using them was that I would always forget what they were.
In the past I would just w
and b
to go forwards and backwards a word at a time until I got close to where I wanted to be.
There's a better way to get where you want to be by jumping to a specific character.
Enter the f
and t
commands.
(T)ouch a character: t
Pretend you're staring at your code and you see the exact letter in the line that you want to place your cursor right in front of. Imagine that you just want to reach out and (t)ouch it.
That's what the t
command does.
t[char] // Move the cursor right before the character that you specify
Note that repeating the command again won't move you to the next instance of the character because the next instance is technically the one that's sitting right after the cursor. That's why I have to use l
to move the cursor one step to the right before executing the command again to get to the next instance of a .
. You'll learn a way around this later on in the repeating commands section.
Touching backwards: T
(Shift+t
)
Sometimes the character you want to go to is behind your cursor.
Applying the Shift
key will perform the behavior backwards. Notice that it places the cursor AFTER the character in this case.
Just remember that t
and T
are trying to just barely touch the character that you specify. They don't want to step on the character, and they definitely don't want to go past it in any way.
(F)lop onto a character: f
Usually when I see a character I want to go to, I want my cursor to be right on top of it. For that we can use the f
command.
I like to think of belly (f)lopping on top of the character.
f[char] // Move the cursor onto the character that you specify
Flopping backwards: F
(Shif+f
)
Going backwards is the same as with the t
command. Just hold down the shift
key and you'll end up on top of the first instance of the character that you specify that happens to be to the left of your cursor.
Repeating the commands more than once
Both f
and t
commands will accept a number modifier before pressing them.
e.g. 2fe
will place the cursor on top of the 2nd e
that it finds after your cursor.
I personally don't use the number modifier often because I don't want to have to scan the entire line for the exact number that I would need to use.
Instead I prefer to use the repeat forward ;
and repeat backwards ,
commands after my initial f
or t
command.
The nice thing about the repeat command vs just repeating your initial touch (t
) command is that it is smart enough to jump and touch the next instance of the character even if the character you're looking for is technically right besides it.
Imagine the cursor is places where the [ ] are:
[T]his is an example sentence
After pressing tetete
your cursor will be here:
This is an[ ]example sentence
Using the repeat command te;;
your cursor will be here:
This is an example [s]entence
Summary
t[char] // Move the cursor right before the character that you specify. Just barely (t)ouch it.
f[char] // Move the cursor right on top the character that you specify. (f)lop on top of it.
The shift
key will perform the movement in the reverse direction.
To repeat the command to find the next instance of the character use ;
to repeat the command going forwards and ,
to repeat the command going backwards.