What's the difference between d
and x
?
As per my cheatsheet, the definition says
x Delete characters after cursor
d Cut but remain in normal mode
I dont know because i'm new to vim
First, let's take a look at the actual documentation for each command instead of your dubious cheatsheet.
:help c
:
["x]c{motion} Delete {motion} text [into register x] and start
insert. […]
:help d
:
["x]d{motion} Delete text that {motion} moves over [into register
x]. […]
:help x
:
["x]x Delete [count] characters under and after the cursor
[into register x] (not |linewise|). Does the same as
"dl".
From which we can make a few observations:
"x
is used as example but it can be any other alphabetic register. The default one is "
. See :help registers
.c
and d
also take an obligatory motion, which makes them operators (see :help operator
), but x
doesn't, so it is not an operator. That is the first difference.d
only deletes the given text while c
also enters insert mode, that is another difference.That's a lot of ways to delete text (and there are others), so when to use one or the other? Well… as everything in Vim, it depends on context and on what you want to do next.
If all you want is to delete a single character, then use x
:
My username is romainnl
^ " position of the cursor
My username is romainl " after pressing x
If you want to delete a few characters that you counted, then use [count]x
:
// I couldn't come up with a better example
^ " position of the cursor
I couldn't come up with a better example " after pressing 3x
If you know your motions well and the text you want to delete is covered by an existing motion (possibly with a count), then use d{motion}
:
Vim is great but antiquated.
^ " position of the cursor
Vim is great. " after pressing dt.
If you want to delete some text and then insert some new text, then use c{motion}
(for "change"):
Vim is the ideal Python IDE.
^ " position of the cursor
Vim is just a text editor. " after pressing ct. and typing just a text editor<Esc>
Now, Vim is kind of infamous for its many commands. On one hand that's quite a lot to learn compared to regular editors, which might seem silly, but on the other hand that is the cost of expressiveness: the more words you know, the easier it is to accurately express your thoughts.
Since you are new to Vim, I strongly recommend that you:
$ vimtutor
a few times until you really get the basics.:help user-manual
.Don't ever rely on random videos, tweets, posts, answers, and threads.
:help
and let us know if there’s something still confusing.:help x
and:help d
will show you what’s different.