2

Background

I am new to vi/vim world and finished running the vimtutor several times.

The setup

In the tutorial we learn several items to correct or edit lines of text.

  • r to replace the letter the cursor is over
  • i, a, A to jump into insert mode
  • ce, c2w, c<#><cmd>, etc - will delete the <#> and enter insert mode.

The question

Is their a way to use r to specific a number of characters to replace?

Similar to c I am looking for something like 2r to replace the next 2 characters. Is this possible? Is their a way to do this with c that I just haven't figured out yet?

  • You can use r in visual mode to replace highlighted characters. So <ctrl-v>2Erx would replace all characters of the current and next word (including space in between) with x. – Rolf Nov 1 '19 at 13:21
7

There are 3 slightly different ways to do this.

  • R will enter "Replace mode". This is like insert mode except each character overwrites the character currently under the cursor. For example

    testing
    ^ Cursor here
    

    and pressing Rrunn<esc> will change the text to

    running
       ^ Cursor here
    
  • Using c. For example, if you want to change 2 characters, you could do 2cl or c2l to delete two characters and enter insert mode. You could also use 2s as s is a synonym for cl

  • The r command can take a count, but this will only work if you want to use the same character. For example, if I have 3 double quotes, and I want to change them to 3 single quotes, I could put the cursor on the first one and type 3r'. Note that R also takes a count. If you typed 3Rabc<esc> it will replace the next 9 characters with abcabcabc.

| improve this answer | |
  • ‘l’ seems to be what I was looking for. Thank you for the reply. And ‘s’ looks like a powerful command to start experimenting with. – danielson317 Oct 30 '19 at 20:20
  • @daniel Glad I could help! s is very powerful indeed. Note that S is a synonym for cc – James Oct 30 '19 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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