Change the next N characters in VIM

VimCharacter

Vim Problem Overview


Say I have the following line:

|add_test()          (| == cursor position)

And want to replace the 'add' with a 'del'.

del|_test()

I can either press X three times and then press i to insert and type del. What I want is something like 3c or 3r to overwrite just 3 characters. Both of these don't do what I want, 3c overwrites 3 characters with the same character, 3r does several other things.

Is there an easy way to do this without manually Xing and inserting the text?

Vim Solutions


Solution 1 - Vim

3s, "substitute 3 characters" is the same as c3l. 3cl and c3l should be the same, I don't know why you'd see the same character repeated. I'm also a fan of using t, e.g. ct_ as another poster mentioned, then I don't have to count characters and can just type "del".

I struggled with the "replace a couple of characters" for a few days too; 'r' was great for single characters, R was great for a string of matching length, but I wanted something like the OP is asking for. So, I typed :help x and read for a while, it turns out that the description of s and S are just a couple of pages down from x.

In other words, :help is your friend. Read and learn.

Solution 2 - Vim

Use c{motion} command:

  1. cf_ - change up to the first '_' (including);
  2. ct_ - change up to the first '_' (excluding);
  3. cw - change the first word;

The word is determined by iskeyword variable. Check it with :set iskeyword? and remove any '_', like that :set iskeyword=@,48-57,192-255. By the way see :help c and :help motion if you want more.

Solution 3 - Vim

I think 3cl is what you want. It changes 3 characters to the right. I'd type ct_del<esc>, but that's not what you asked

Solution 4 - Vim

c3  ('c', '3', space), then type the characters you want to insert. (Or you can use right-arrow or l rather than space.)

Or, as @Mike just said in a comment, R works nicely if the number of characters happens to match the number of characters you're deleting.

Or ct_ to change from the cursor to the next _ character.

Or, as @bloody suggests in a comment, 3s.

Solution 5 - Vim

If the works have the same length you can use the R command which replaces what you had previously with what you type.

Solution 6 - Vim

The other answers given use numbers. When the text is longer it's easier to not have to count. For example I often make headlines in markdown files like:

double the line with yy pp

Some super duper long title that I don't want to have to count

Highlighth the whole line with V then use r{char} or in this case r= to get:

 ==============================================================

(I added a space above to trip stack overflow's markdown formatting)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMrBView Question on Stackoverflow
Solution 1 - Vimdash-tom-bangView Answer on Stackoverflow
Solution 2 - VimlumberjackView Answer on Stackoverflow
Solution 3 - VimajkView Answer on Stackoverflow
Solution 4 - VimKeith ThompsonView Answer on Stackoverflow
Solution 5 - VimskeeptView Answer on Stackoverflow
Solution 6 - Vimuser2162559View Answer on Stackoverflow