vim – 是否可以在运算符挂起的映射中确定运动是线性,块状还是正常?

前端之家收集整理的这篇文章主要介绍了vim – 是否可以在运算符挂起的映射中确定运动是线性,块状还是正常?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当使用omap或onoremap声明映射时,我希望能够处理运动将是blockwise,linewise或standard的情况.

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

abcd
efgh
ijkl
mnop

光标位于字母f上.假设我将K的运算符映射定义为:normal! vjl(转到字母k).

onoremap K :normal! vjl<cr>

有趣的是,当我运行dvK,dK,d ^ VK时我分别得到了

abcd   abcd   abcd
el     el     eh
mnop   mnop   il
              mnop

但是当我运行dVK它不起作用时,我和dvK完全一样.

我尝试使用visualmode()(映射定义为@ = visualmode()< cr> jl< cr>但这不起作用.看来当你使用v,V时,这个函数的返回值不会立即受到影响CTRL-V处于操作符挂起模式.

有人有线索吗?
谢谢

我已经写了一些关于运算符挂起映射的答案.在 one of them1中,我根据文档概述了应该处理各种情况(char,line,block wise选择)的函数的大纲:
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 call a function from inside a vmap

原文链接:https://www.f2er.com/bash/384887.html

猜你在找的Bash相关文章