This little c
is a very kind specimen and can save you a lot of time. First
let’s see what :help
tells us about it.
["x]c{motion} Delete {motion} text [into register x] and start insert.
Basically delete content based on the provided motion and switch to insert mode.
An easy way to remember: c
as [c]hange
.
The {motion}
can be anything that falls under the motion category, even our
lovely t/T/f/F
from day 1.
That ["c]
means register, we will talk about it later, not just ignore it.
Examples
Replace string in code
Let’s say we have this rude line, and we want to make it move friendlier by
replacing Goodbye cruel land!
with Welcome aboard mate!
. We don’t care where
the cursor is because we can always start out work with 0
that moved the
cursor to the beginning of the line.
fmt.Printf("Goodbye cruel land!")
First we want to move to the string part, we can do that with f"
, and now we
can simply call ci"
(change inside "
). And that’s it, we already in insert
mode, so we can just type our new message Welcome aboard mate!
.
fmt.Printf("Welcome aboard mate!")
That was easy. What if we want to replace the whole string definition with a
constant. We defined out constant at the top of the file (because we are
cultured people) with a name like welcomeMessage
.
We have the same initial line again:
fmt.Printf("Goodbye cruel land!")
We start with 0
again, that way we don’t have to care about the position of
the cursor. We don’t have to do it, I do it here because it’s easier than
describing where the cursor is.
With the same motion as before, we can move to the string definition f"
, but
this time we use a bit different option and we pick the magical ca"
(change
around "
). Yes, that’s it, as before, we are already in insert mode, so we can
type in the name of our new constant.
fmt.Printf(welcomeMessage)
Replace words
When we are writing an email or meeting notes, we may want to replace a few
words. We can press backspace
or delete
, we can delete them with d
and
then switch to insert mode, but we know c
does exactly that. Our example is
snippet from an email.
I sent you my questions 8 days ago, can you answer them please?
That 15 days sounds oddly specific and can indicate “you better answer, I’m counting the day and I know where you live” tone and even if that’s the case, we may want to avoid this. We can replace a much friendlier “last week” phrase.
(Same reasons, we start with a 0
)
With f8
we are already on the character 8
, now we have to change those three
words, we can get this with c3w
(change 3 words). As before, we are already in
insert mode.
I sent you my questions last week, can you answer them please?
Special
As most of these actions (c
, i
, a
, d
), they have an upper-case version
and that’s usually do things about a whole line:
C
: Change from the cursor to the end of the line.I
: Switch to insert mode and place the cursor at the beginning of the line.A
: Switch to insert mode and place the cursor at the end of the line.D
: Delete from the cursor to the end of the line.