Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To letter, then delete until letter, and repeat

Tags:

vim

From "practical vim", I gather than a good practice is to try to move, act, and then repeat.

Say I have this string:

foo_bar fooo_bar foo abar foo_bar

I would like to move forwards until I find an f, and then delete until I find a b, and repeat.

I would have thought that the following would work:

ff
dtb
;
.
;
.

ff would be the command to move, and then dtb the one to act.

However, when I press ;, it goes forwards until just before the next b, while I would like to repeat my "move" command, i.e. ff.

Is there a way to do this, such that the "act" command doesn't change the behaviour of the "move" one?

like image 291
ignoring_gravity Avatar asked Nov 18 '25 02:11

ignoring_gravity


1 Answers

; repeats the latest fFtT which, in your case, is tb, not ff. There is no way to make ; repeat something else.

Here are alternative methods…

  • With :help /:

    ff
    d/b<CR>    " delete until next b
    ;
    .
    ;
    .
    

    As mentioned by @mattb, the trick is to use a different motion than any of fFtT for the operation so that your ff is always the latest motion repeatable with ; or ,.

  • With :help recording:

    qq         " start recording in register q
    ff
    dtb
    q          " stop recording
    @q         " play it back
    @q         " play it back
    

    The trick is to encapsulate the initial motion and the operation into a single macro that can be repeated over and over without involving ; at all.

like image 150
romainl Avatar answered Nov 21 '25 09:11

romainl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!