What do the f and t commands do in Vim?

What do f and t commands do in vim and exactly how they work?


Solution 1:

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 find and till. 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.

Solution 2:

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.
                 ^