3

I was speaking with my friend about removing multiple lines while using vim. They said they use d3d to remove 3 lines, whereas I use 3dd

I understand my command is: 3 (repeat 3x) dd (delete the entire line)

And my friend's command is of the form ["x]d{motion} i.e. Delete text that {motion} moves over [into register x].

However, I don't think 3d is a valid motion. Is it? I found this page which is expessing the same confusions how can i use dcountd but it diverges into omaps and expression mappings which I am not interested in - I don’t want to change the behaviour I just want to understand it (and also find out where my friend would have found the command)

I don't understand why this d3d command works in vim.

2
  • 3
    The question title has a typo: 3d3 should be d3d. May 31 at 6:44
  • 1
    for non-prime numbers, there's even an additional option- 2d2d = 4dd = d4d.
    – Mass
    May 31 at 15:49
5

The page that you linked to is a useful read. Like you said, not all of it is relevant. What is relevant is the first sentence in that answer:

Between typing an operator (like d, c, or gU) and a motion (like w, i}, or /foo), Vim is in Operator-pending mode.

In dd, after the initial d, vim is in operator-pending mode. Pressing d again is akin to the _ motion. This is a linewise motion which moves the cursor to the start of the current line. Thus, the final d would mean "apply this operator (d) to the current line".

Now, in d3d, after the initial d, vim is, again, in operator-pending mode. In this mode, 3d is likened to 3_. Put together, 3d would mean "apply this operator (d) for 3 lines."

tl;dr [count]d is not valid in normal mode, but it is valid in operator-pending mode when the current operator is d.

2

This looks as consistent behaviour among Vi clones. Consider the following excerpt from POSIX Vi spec.:

motion An optional trailing argument used by the !, <, >, c, d, and y commands, which is used to indicate the region of text that shall be affected by the command. The motion can be either one of the command characters repeated or one of several other vi commands (listed in the following table). Each of the applicable commands specifies the region of text matched by repeating the command; each command that can be used as a motion command specifies the region of text it affects.

So basically while you're typing d command, the next d is treated as "motion", and so both d3d and 3dd shall be interpreted exactly the same. However, this is really rare input to see and to use.

1
  • This answer is completely valid but I can only mark 1 answer. Other answer makes the distinction that Motions in Normal and Operator-pending modes can be different which is the key piece of information I personally was missing.
    – Crusty
    May 31 at 23:33

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.