![](/img/trans.png)
[英]How to remove the highlight from a search inside an `operator-pending` mapping in vim ?
[英]Is it possible to determine in an operator-pending mapping whether the motion will be linewise, blockwise or normal?
当用omap
或onoremap
声明映射时,我希望能够处理运动是omap
, onoremap
或标准的情况。
例如,让我们考虑以下块:
abcd
efgh
ijkl
mnop
光标在字母f上。 假设我定义了一个从K
到:normal! vjl
的运算符映射:normal! vjl
:normal! vjl
(转到字母k)。
onoremap K :normal! vjl<cr>
有趣的是,当我运行dvK
, dK
, d ^V K
我分别得到
abcd abcd abcd
el el eh
mnop mnop il
mnop
但是,当我运行dVK
,它将不起作用,与dvK
。
我试图使用visualmode()
(映射定义为@=visualmode()<cr>jl<cr>
但这无法正常工作。当您使用v
, V
或CTRL时,此函数的返回值似乎不会立即受到影响。 -V在操作员待定模式下。
请问有人有线索吗?
谢谢
要实现您想要的,您只需定义
onoremap K :<c-u>normal! jl<cr>
请注意,此运动是由命令执行的,始终是按字符进行的(请参阅:h movement
然后,您可以自由地使用dv
, dV
或d^V
强制将运动设为另一种类型并获得所需的内容。
我已经写了一些关于操作员待定映射的答案。 在它们中的一个 1 I勾勒应该处理的各种情况下(字符,线,框明智选择)根据文档的功能的概要:
g@{motion} Call the function set by the 'operatorfunc' option.
The '[ mark is positioned at the start of the text
moved over by {motion}, the '] mark on the last
character of the text.
The function is called with one String argument:
"line" {motion} was |linewise|
"char" {motion} was |characterwise|
"block" {motion} was |blockwise-visual|
Although "block" would rarely appear, since it can
only result from Visual mode where "g@" is not useful.
{not available when compiled without the |+eval|
feature}
这是一个用<F4>
计算空格数的示例:>
nmap <silent> <F4> :set opfunc=CountSpaces<CR>g@
vmap <silent> <F4> :<C-U>call CountSpaces(visualmode(), 1)<CR>
function! CountSpaces(type, ...)
let sel_save = &selection
let &selection = "inclusive"
let reg_save = @@
if a:0 " Invoked from Visual mode, use '< and '> marks.
silent exe "normal! `<" . a:type . "`>y"
elseif a:type == 'line'
silent exe "normal! '[V']y"
elseif a:type == 'block'
silent exe "normal! `[\<C-V>`]y"
else
silent exe "normal! `[v`]y"
endif
echomsg strlen(substitute(@@, '[^ ]', '', 'g'))
let &selection = sel_save
let @@ = reg_save
endfunction
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.