3

When creating an operator pending mapping in vim, it is possible make the mapping work on an arbitrary (consecutive) piece of text.

I am looking for a way to abort the operation. For the case of deletion (e.g. di)) this can be done by having the mapping do nothing. The cursor will not be moved and nothing will be deleted.

However when using a change command (e.g. ci)) one is left in insert mode. To illustrate a bit more the following commands can be run in Vim:

:function! Foo()
  :return
:endfunction
:onoremap i) :call Foo()<cr>

By issuing ci) in normal mode on the text (bar) it is immediately clear that one is left in insert mode.

My question now is whether it is possible to change the function in such a way that the mapping will not have any effect. In practice the function will of course use logic to determine whether it should abort or not.

For what it's worth, I'm running gVim 7.4 32-bit on windows 7. The end goal of this particular mapping is to remap i) and the like such that they will select inside the next set of parenthesis if the cursor is not currently inside any parenthesis. If anyone has a suggestion for doing this some other way, it would of course be welcome, but I would still be interested in the answer to my original question.

1 Answer 1

3

This works for me:

:function! Foo()
  call feedkeys("\<esc>")
  return
:endfunction

This works since you can hit esc in operator pending mode (for example, c<esc>), and it will not complete the operator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.