I have never used :h :command-register and :h <reg> , trying to find an example, but failed to grep \-register from my installed plugins,

The built in :h :delete support a count and an optional register:

:[range]d[elete] [x] {count}
            Delete {count} lines, starting with [range]
            (default: current line |cmdline-ranges|) [into
            register x].

How to create similar command?

Failed Attempt

function s:delete(reg, count) abort
  let reg = empty(a:reg) ? '"' : a:reg
  exe 'delete' reg a:count
command -nargs=1 -register Delete call s:delete('<reg>', <f-args>)


Delete 3
E471: Argument required: Delete 3

Delete 30
Error detected while processing function <SNR>100_delete:
line    2:
E488: Trailing characters: delete 3 0

Delete a 3
works fine

It doesn't look like optional at all, it looks like it always use 1st char or 1st arg as register.

1 Answer 1


The problem with -reg is that anything that looks like a register is parsed as such, even when there's no separator to the following arguments. So :Delete 30 sets <reg> to 3 (numbered register) and 0 as the argument. Unfortunately, :delete itself seems to use different parsing rules; it doesn't accept a numbered register, so instead of getting E939: Positive count required you see E488.

Because of these problems, I tend to skip -reg and do the parsing on my own (using ingo#cmdargs#register#ParsePrependedWritableRegister() from my ingo-library plugin), enforcing whitespace separation between the register and further arguments. That may explain why you haven't found any plugins using -reg.

To make :Delete 3 work, you either have to use custom parsing that does not treat 3 as a register, or make the arguments optional via -nargs=?.

  • 1
    Should it be E488 in the end of 1st paragraph? I'm not sure i completely follow, I guess i need some time to digest it.
    – dedowsdi
    Nov 14, 2019 at 13:36
  • Yes E488; corrected. Nov 14, 2019 at 13:41

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.