簡體   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