VIMTIP! Getting around with f & t

Here’s a quick one that will get you ripping through lines faster than Scarface on a good day. When moving through a line, w & b will get you there faster than the single movement keys (h & l), but more times than not, will result in repeated keystrokes. Anytime I find myself hitting the same key over and over again in Vim, I know there’s got to be a better way to get it done. It’d be much faster if we could target the specfic character we’re looking to get at. f & t do just that!

  • f{char} moves the cursor to the next occurance of the char, including the char itself. F does the same thing, but in the opposite direction.
  • t{char} moves the cursor right before the next occurance of the char. T does the same thing, but in the opposite direction.

After you have performed one of these movements, you can use ; to get to the next occurance in that line and , to the previous occurance.

Let’s take a look at an example and see it in action.

Vestibulum izzle tortizzle. Pellentesque gangster rhoncus. Go
bizzle we gonna chung platea sure. Pimpin' dapibus. Gizzle
pretium rizzle, mattizzle ac, i'm in the shizzle vitae, nunc.

Say our cursor is on the first line and we want to get to the period. We could www or 3w and that would move us right after tortizzle and before the . If we wanted to get to the next period, we’ll either need to w our way there or guess at the count and hope it’s right. Or we just f. and that will take us to the same exact spot. If I hit ;, that’ll take me to the next period in that line.

If you look at line three, I’ve got an improperly capitalized i'm. If I drop down to line 3, and hit t' that will move my cursor to right before the apostrophy and on our offending character. A little rI to fix the mistake and I’m on my way.

You can also use these with movement commands (d, c, or v) and make editing text more efficent. I’ve found targeting certain characters has made traversing through lines much faster than guessing with count & movement commands. Which has made f & t my goto’s for getting around.

Details: :help f or :help t

As my grandad always used to say:

You can’t dot w’s and b’s, but you can sure cross f & t’s.

I suppose so… Happy Vim'ing!

comments powered by Disqus