繁体   English   中英

是否可以在待处理的映射中确定运动是直线运动,块运动还是法线运动?

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

当用omaponoremap声明映射时,我希望能够处理运动是omaponoremap或标准的情况。

例如,让我们考虑以下块:

abcd
efgh
ijkl
mnop

光标在字母f上。 假设我定义了一个从K:normal! vjl的运算符映射:normal! vjl :normal! vjl (转到字母k)。

onoremap K :normal! vjl<cr>

有趣的是,当我运行dvKdKd ^V K我分别得到

abcd   abcd   abcd
el     el     eh
mnop   mnop   il
              mnop

但是,当我运行dVK ,它将不起作用,与dvK

我试图使用visualmode() (映射定义为@=visualmode()<cr>jl<cr>但这无法正常工作。当您使用vV或CTRL时,此函数的返回值似乎不会立即受到影响。 -V在操作员待定模式下。

请问有人有线索吗?
谢谢

要实现您想要的,您只需定义

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

请注意,此运动是由命令执行的,始终是按字符进行的(请参阅:h movement

然后,您可以自由地使用dvdVd^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

1个 vim从vmap内部调用函数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM