Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to determine in an operator-pending mapping whether the motion will be linewise, blockwise or normal?

Tags:

vim

When declaring a mapping with omap or onoremap i would like to be able to handle the case where the motion will be blockwise, linewise or standard.

For example let's consider the following block:

abcd
efgh
ijkl
mnop

Cursor is on the letter f. Suppose that I define an operator map from K to :normal! vjl (go to letter k).

onoremap K :normal! vjl<cr>

Interestingly enough when I run dvK, dK, d^VK I get respectively

abcd   abcd   abcd
el     el     eh
mnop   mnop   il
              mnop

But when I run dVK it won't work, I get exactly the same as with dvK.

I tried to use visualmode() (mapping defined as @=visualmode()<cr>jl<cr> but this does not work. It seems that the return value of this function is not immediately affected when you use v, V or CTRL-V in operator-pending mode.

Does anyone have a clue please?
Thank you

like image 475
Benoit Avatar asked Dec 19 '11 16:12

Benoit


1 Answers

To achieve what you desired, you can simply define

onoremap K :<c-u>normal! jl<cr>

Note this motion, as formed by a ex-command, is always characterwise (see :h movement

Then you can freely use dv, or dV, or d^V to force the motion to be another type and get what you want.

like image 121
doraemon Avatar answered Oct 02 '22 20:10

doraemon