2

Newbie question: Are counts commutative?

2d2w is the same as 4dw, right?

Is it the case for all commands that (count1)(verb)(count2)(whatever) the same as (count1+count2)(verb)(whatever)?

If counts are commutative, why are there two places to give a count?

  • 1
    What happens when you press . after each of your commands? – D. Ben Knoble Oct 5 '18 at 23:23
2

[please forgive me for the poor formatting, I typed it in my cell phone]

Generally speaking, it is not necessarily always the case.

User can define new operators, which sets the operatorfunc and then run normal command g@. For example

nmap D :let g:count=v:count1 <cr>:set operatorfunc=MyD<cr>g@

It is the user’s responsibility to capture the count (v:count1) pressed before pressing D, save it and then handle it inside function MyD

After pressing D, vim then enters the operator pending mode, waiting for a motion. This motion can also handle it own count. For example,

omap <expr> D v:count . “j”

That means pressing 3D in operator pending mode means the same as 3j, which selects 4 lines. But how does the operator D handles these 4 lines? It actually don’t know here 3j is pressed. Inside MyD, the marks ‘[ and ‘] are set to the start and end of the selection. This is the info passed to MyD from the motion.

If user press 2D5D, inside MyD, it knows the marks and knows the count g:count and perform the real action.

From all these, you know that generally it doesn’t need to be commutative. A lot logics can be defined.

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.