0

I want to replace a sequence of 6 hex digits (hex RGB) with new values, but I want to repeat a 2-tuple 3 times.

For example:

Start: #ea9081 (with the cursor on 'e')

Desired: #f0f0f0

I know I can do 6rf, as an example to change all 6 values to 'f', or dte3if0<esc> to delete to end of word and repeat-insert 3 times. But is there a way to modify something along the lines of 3cef0<esc> to repeat a change three times? Or some magic with s (substitute)?

3
  • 2
    I'd just use cw and then f0f0f0; you can probably be more "clever" about it, but you'll spend more time on the cleverness than you'd save repeating f0 two times. May 6, 2020 at 12:00
  • 1
    @MartinTournoij Yeah I guess this question makes more sense for a snippet longer than two characters and/or repeated way more than 3 times :-)
    – filbranden
    May 6, 2020 at 15:31
  • 1
    Good point about the time spent, but I just felt like the general case of repeating sequences of characters must exist in vim. May 6, 2020 at 18:23

1 Answer 1

2

You can use 3Rf0<Esc>.

The R command is similar to r, but it replaces multiple characters and not just a single one. It takes an <Esc> to leave Insert (Replace) mode.

You can also use . to repeat a Replace action, and you can repeat it with a count. For example, you can use Rf0<Esc> to replace the first instance, followed by l2. if you realize after the first replacement that you'd like to have it done two more times.

4
  • 3
    Not any shorter, but easier for my brain: Rf0<esc>.. (may need some ls to move right)
    – D. Ben Knoble
    May 6, 2020 at 14:16
  • 3
    @D.BenKnoble You actually need to move the cursor here. Rf0<Esc>l.l. works, or better Rf0<Esc>l2. if you realize after the replacement that you'd like to do it two more times.
    – filbranden
    May 6, 2020 at 15:30
  • 1
    I was also trying to figure this problem out with ., but couldn't get it to work with my old thinking. Replace-mode does the trick! May 6, 2020 at 18:30
  • 1
    @jdk1.0 Updated the answer to include the possibility of using 2. here too!
    – filbranden
    May 6, 2020 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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