Skip to main content
edited tags
Link
Rich
  • 32.6k
  • 4
  • 74
  • 141
Source Link
DJMcMayhem
  • 17.9k
  • 5
  • 60
  • 87

How to allow count *before* my custom operator?

I'd like to define my own operator. Vim's builtin help has a nice little tutorial on it, where they show you can create your own operator with opfunc and g@:

nmap <silent> <F4> :set opfunc=CountSpaces<CR>g@
vmap <silent> <F4> :<C-U>call CountSpaces(visualmode(), 1)<CR>

function! CountSpaces(type, ...)
  ...
endfunction

There's only one downside to this approach. Let's say for example, that I want to run <F4>2j. That works fine, but 2<F4>j does not. This gives

E481: No range allowed

This makes perfect sense because the range is applied to the set opfunc command, not the g@ command.

I thought that I would be able to get around it by doing this:

nnoremap <expr> <F4> ":\<C-u>set opfunc=Test\<cr>".v:count1.'g@'

But when I run this, it causes some strange issues where '[ and '] are incorrectly defined in the function. (I'd be happy to go into more details in chat, but that's unrelated to my question. I've reproduced this bug in Vim 8.0 on Windows and Ubuntu and in Neovim 0.2.1 on Windows)

So how can I define my operator so that a count works the same before or after the operator? I realize that this is a very minor difference, and that for most intents and purposes, I can simply get around it by always doing the count afterwards. But vim's default operators work this way, and I would really like mine too also.