加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 01:14:35 所属栏目:安全 来源:网络整理
导读:当使用omap或onoremap声明映射时,我希望能够处理运动将是blockwise,linewise或standard的情况. 例如,让我们考虑以下块: abcdefghijklmnop 光标位于字母f上.假设我将K的运算符映射定义为:normal! vjl(转到字母k). onoremap K :normal! vjlcr 有趣的是,当我
当使用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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读