3

Is there some way to repeat something like cit and be left in insert mode instead of automatically repeating the previously entered text?

For example, suppose I have the following lines:

<th>one</th>
<th>one</th>
<th>one</th>
<th>one</th>

I'd like to individually replace the text inside the last three tags so I end up with:

<th>one</th>
<th>two</th>
<th>three</th>
<th>four</th>

If I start by going to the second line and type cittwo<esc>, it'll replace "one" with "two". If I then go to the next line and press ., it'll repeat the last command and again replace the "one" with "two". Instead, I was hoping for a way to just delete the contents of the tag and be left in insert mode so I can type whatever I needed.

I know I could make a macro that does this, but for some reason I feel like there is a more basic way of doing it that is currently escaping me.

1 Answer 1

2

There isn't really a native command to repeat a change without including the inserted text as part of the repeat.

You mentioned using a macro, but recording it (using the q command) can be tricky, since you want to end it inside insert mode. It turns out you can actually do that using qacit<C-O>q to record only cit in @a, which you can use to repeatedly call it. You can also use an assignment directly to create the macro.

:let @a = 'cit'

Not sure if the macro really gets you anything, since you're still typing two characters @a instead of three cit. Granted, after the first usage, you can simply hit the same key twice @@, but that's not a huge win...

Perhaps creating a mapping temporarily would be better:

:nnoremap , cit

Or you can even map . so it "feels" like you're repeating a command 😁

Once you're done, remove it with :nunmap , (or ., or whatever you decide to use.)

One advantage of using a mapping (or a macro) is that you can incorporate the search for the next location to replace, whether you simply use a n command or if you use a text object more aware of HTML tags to find the location. In that case, you'll be gaining more than a couple keystrokes by defining a macro or mapping.

Your Answer

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

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