Say we have the following:

echo 'This is a test'.$test1.$test2.$test3.$test4.$test5.'and this is another test';

With the cursor being on the first dollar sign, and I want to change everything between all dots:

echo 'This is a test'..'and this is another test';

what is the fastest way to do this? Preferably using f, F, t, T.

c5t. works, but requires you to count exactly how many dots there are. Unfortunately, something like c99t. simply won't work—it won't skip to the last dot.

  • I would go with c/..and<CR> Using search to match two characters before and. Actually that could even be c/..a<CR> if we are playing vimgolf. But that doesn't work in all the cases. – statox Mar 3 at 14:24
  • 2
    v$T.c? But I remember having answered with a text object for these kind of manipulations in the past :/ ... – Luc Hermitte Mar 3 at 14:25
  • 1
    Welcome to Vi and Vim! Thanks for the edit! – D. Ben Knoble Mar 3 at 15:18

I find it is best to not get too wrapped up in optimizing for minimal keystrokes, but instead learning & using motions effectively. Sometimes this means multiple motions, multiple operations, and even using ex-commands. The best way is typically what keeps you doing your work and not thinking about golfing your key strokes

Let's look at some ways to accomplish your goal

  • cf5 - uses f to find the 5 character. Great for this exact case, but not general
  • Using v, t., and ; - Builds up the visual selection and repeat t. motion with ;. Simple but does require hammering the ; key
  • ct. then do d; or v; to clean up the line (repeat with ./; as needed)
  • ci' - Two dots are easy to type, so just change everything inside the quotes
  • Use / to search directly. e.g. c/.*\zs\.<cr>
  • v$F.lc - Use visual mode and search from end of line with F
  • v$?.\.<cr>c - Use ? to search backwards from end of line
  • :s/\.\zs.*\ze\./replace/ - substitute using \zs & \ze
  • etc

Ultimately which ever way you remember is best. I think using ci' and vt.;;;;c would probably fit with how I think best. Using / would be ideal if the search is easy. Another consideration is if you are going to repeat this change then finding the ideal motion or pattern would change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.