Vim `f` and `t` Motions
The f
and t
motions in Vim are incredibly useful for navigating within a line. They allow you to jump to a specific character’s occurrence. While seemingly simple, mastering these motions can significantly boost your editing efficiency.
f{char}
(find)
The f
motion moves the cursor forward to the next occurrence of the specified character {char}
on the current line.
For Example:
Given the line: const message = "Hello, world!";
Typing f;
would move the cursor to the semicolon at the end of the line. Typing fo
would move the cursor to the ‘o’ in “world”. Subsequent presses of ;
(semicolon) will repeat the last f
motion, moving to the next ‘o’ in “Hello.” ,
(comma) would move backward to the previously found ‘o’.
t{char}
(to)
The t
motion is similar to f
, but it moves the cursor to the character before the specified character {char}
on the current line.
For Example:
Using the same line: const message = "Hello, world!";
Typing t;
would move the cursor to the !
(exclamation point). Typing to
would move the cursor to the ‘r’ in “world”. Like f
, the ;
and ,
keys repeat and reverse the t
motion respectively.
Combining with Operators
The real power of f
and t
comes when combined with Vim operators.
Deleting:
df;
would delete from the current cursor position up to and including the next semicolon.dt"
would delete up to, but not including, the next double quote.Changing:
cf,
would change text from the current cursor position up to and including the next comma.ct!
changes up to, but not including, the next exclamation point, placing you in insert mode.Yanking:
yf.
yanks from the current cursor position to and including the next period.
Example in JavaScript (Illustrative):
Imagine you have this JavaScript code:
const longVariableName = someFunction(argument1, argument2, argument3);
You want to change argument2
to something else. Using f,
twice positions you at the second comma. Then, ct)
changes the text up to the closing parenthesis, leaving you in insert mode to type the new argument.
By understanding and utilizing f
and t
, you can precisely navigate and manipulate text within a line, significantly improving your editing speed and accuracy in Vim.