A Tale of Two (Vim) Commands: "s" and "c"

Comments

6 comments posted
"r" is great ... if you want a *:1 character replacement

Paul and Gabe,

You guys are absolutely right about ``r``. I love ``r``. I use ``r`` all the time. It is one of those things that if Vim did not provide out of the box, I would have had to script it.

*BUT* it is only useful if you are replacing *with* a single character. Sure, you can make a visual selection and hit "r" and then the replacement character to replace all characters in the selection with the character. And then there is magic like the following:

(1) type in a line: "ohello, world"
(2) type in "yyPVr="

and presto! we have underlining.

However, when you want to replace with *more* than one character, and the text is not in any register has to be typed fresh, then ``s`` or ``c`` are the way to go.

Posted by Jeet Sukumaran on Fri, 09/24/2010 - 15:27
Mo'r'e efficient way of replacing a single char

"i.e., replacing a single character with new text to be typed in, "s" is the most efficient way to do things."

Using 'r' to (r)eplace a single character is even more efficient since you don't have to escape out of insert mode afterwards!

Posted by Paul King (not verified) on Thu, 09/23/2010 - 19:03
"* When you want to delete

"* When you want to delete the character under the cursor, and replace it with text that you want to type in (which may be more than one character)."

Let's not forget `r`! Your bullet here says "which may be more than one character." Indeed, when you want to replace a single character with more than one character then `s` seems like the way to go. But if you're replacing with exactly one character, `r` is even better since you don't have to leave normal mode.

Posted by Gabe (not verified) on Mon, 09/20/2010 - 16:56
vim

I am a VIM noob but I think if you use cw to change a word you can repeat the command like a macro on different size words. Whereas s is more of a global search and replace

Posted by Bob Hazard (not verified) on Sun, 09/05/2010 - 08:56
You are mistaking :%s for s.

You are mistaking :%s for s.

Posted by Anonymous (not verified) on Thu, 09/16/2010 - 17:54
Partially correct

If you use "cw", you can use "." (=="repeat" command) to repeat the same action on the next word. And this repeated action will be both: (a) the deletion of the word and (b) the insertion of whatever characters you typed in its plac.

With "s", if there is no visual selection, invoking the repeat command will result in a single character being deleted. And that's it. You lose the repeatability of the full edit.

If there is a visual selection and you use "s", the behavior is the same as if you used "c".

I do not know what you mean with the "global search and replace" reference. I do not see any relationship between "s" and this.

Posted by Jeet Sukumaran on Sun, 09/05/2010 - 13:11

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a biological visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.