What do the f and t commands do in Vim?

Vim

Vim Problem Overview


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

Vim Solutions


Solution 1 - Vim

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 - Vim

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

Solution 3 - Vim

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

Solution 4 - Vim

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, fFand 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.

Solution 5 - Vim

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDanView Question on Stackoverflow
Solution 1 - VimMichael KristofikView Answer on Stackoverflow
Solution 2 - VimLondonRobView Answer on Stackoverflow
Solution 3 - VimVaughn CatoView Answer on Stackoverflow
Solution 4 - VimromainlView Answer on Stackoverflow
Solution 5 - VimrranjikView Answer on Stackoverflow