Change the following N characters in VIM

Let's say I have the following line:

|add_test() (| == cursor position) 

And I want to replace 'add' with 'del'.

 del|_test() 

I can either press X three times, and then press i to insert and enter del. What I want is something like 3c or 3r to overwrite only 3 characters. Both of them do not do what I want, 3c overwrites 3 characters with the same character, 3r does a few other things.

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

+59
vim character
Aug 29 2018-11-11T00:
source share
7 answers

3s , "substitute 3 characters" matches c3l . 3cl and c3l should be the same, I don’t know why you will see the same character. I am also a fan of using t , for example. ct_ as another poster is mentioned, then I don’t need to count the characters and just type β€œdel”.

I struggled with "replacing multiple characters" for several days; "r" is great for single characters, R great for a string of matching length, but I need something like OP. So, I typed :help x and read for a while, it turned out that the description of s and s is only a couple of pages down from x .

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

+93
Aug 29 '11 at 23:52
source share

Use the c {motion} command:

  • cf _ - change to the first '_' (including);
  • ct _ - change to the first '_' (excluding);
  • cw - change the first word;

The word is defined by the iskeyword variable. Check this with : set the "keyword" and delete all the "_", for example : set iskeyword = @, 48-57,192-255 . By the way, see : help c and : help motion if you want more.

+24
Aug 29 2018-11-23T00:
source share

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

+7
Aug 29 '11 at 23:45
source share

c3 ('c', '3', space), then enter the characters you want to insert. (Or you can use the right arrow or l instead of a space.)

Or, as @Mike just said in a comment, R works just fine if the number of characters matches the number of characters to delete.

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

+6
Aug 29 2018-11-23T00:
source share

If the jobs are the same length, you can use the R command, which replaces what you had before with what you type.

+3
Aug 30 2018-11-11T00:
source share

Other answers are given using numbers. When the text is longer, it is easier not to read it. For example, I often make headings in markdown files, for example:

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

double line with yy pp

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

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

Some super duper long title that I don't want to have to count ============================================================== (I added a space above to disable markdown formatting)

+1
Aug 18 '18 at 19:00
source share

"c3h" will return 3 characters and go into insert mode, then enter your "Del"

0
Jun 01 '19 at 1:14
source share



All Articles