s
shortcut20 September 20
If you have used vim for quite some time you are probably familiar with all the different ways to change text. There’s c
for change, d
for delete which make up the bread and butter for normal mode editing operators. Out of these two commands spring shortcuts that are very organic and not very programmatically sensible.
Most famously there is x
which is equivalent to dl
. Or put simply, it deletes the character under the cursor. It is almost always used more often than dl
even though dl
is only one character more.
My guess is that the mental idea of dl
does not flow with how vim users tend to think when editing text. When vim users edit in normal mode, we think of applying an operator to a movement. Which is why most vim users tend to type d3w
when they want to delete three words rather than doing 3dw
which is synonymous with deleting one word three times. The l
movement means to move the cursor to the right by one character, and together with d
it means to delete until the next character.
I don’t know about you, but that makes a lot less sense in my head than the concept of deleting the character under my cursor. Since dl
is only one character longer than x
and only exists in the home row, my guess is that the speed trade-off is minimal to none. I think x
exists more as a mental shortcut abstraction rather than merely a keyboard shortcut. There are other commands such as D
which deletes from your cursor to the end of the line, and C
which does the same, but puts you in insert mode after. To me, these are more keyboard based shortcuts, and have some sort of logic to them. The default $
for movement to the end of the line is not conducive to typing flow. However, the thought process is the same for D
and d$
.
The law of capitalising an operator to denote its operation to the end of the line is somewhat universal in vim. This does not apply to Y
however, which is why the vim help page also recommends you to remap it.
Much lesser known however, is s
. I would categorise this command as a mental keyboard shortcut which is a combination of both being much faster than its alternative in keyboard flow as well as being much simpler when it comes to mental effort required.
The s
shortcut is short for cl
. It deletes the character under the cursor, then puts you in insert mode. I personally urge any vim user that does not currently use this normal mode command to force yourself to use it.
I have found that I find myself in situations where I need to use s
quite often when editing text. But before being familiar with it, I would never have thought that cl
was what I needed to type, because to me cl
makes less intuitive sense in text editing terms. It means to change until the next character. But that doesn’t sound like what I want to do.
Novice vim users would have used something like xi
, which is what I would have used. But xi
is quite a bit worse than s
because both keys are off the home row.
Please force yourself to use the s
shortcut (which I think stands for “substitute”). You will feel quite a bit faster, trust me.
Best Regards,
Philip