【问题标题】:Vim - how to jump to start/end of paragraph (which works in all cases)Vim - 如何跳转到段落的开头/结尾(适用于所有情况)
【发布时间】:2021-05-30 07:27:29
【问题描述】:

真的没有简单的映射可以跳转到段落的第一行或最后一行吗?

{} 是“独占”命令,因此它们会跳转到段落之前或之后的空行如果有,否则会跳转到实际的第一行或最后一行段落(即,当段落位于缓冲区的顶部或底部时)。

这种差异似乎无法以任何直接的方式进行补偿,例如{w 如果前面有空行,则跳转到段落的开头,但对文件中的第一段执行错误的操作。

【问题讨论】:

    标签: vim paragraph


    【解决方案1】:

    Vim 中暂时没有这样的动作。

    尽管在:h todo 中它被标记为 7(即 ASAP):

    7   Add "g{" and "g}" to move to the first/last character of a paragraph
        (instead of the line just before/after a paragraph as with "{" and "}").
    

    如果我们只限于当前段落,那么我可以提出临时解决方案:

    nnoremap g{ vipo<Esc>
    nnoremap g} vipoo<Esc>
    

    如果我们想要更接近真实的东西,那么我们可以使用函数:

    function! Foo(x,y) abort
      if empty(getline(line('.') + (a:x == '{' ? -1 : 1)))
         exec "norm! ".a:x
       endif
      exec "norm! ".a:x
      if empty(getline('.')) | exec "norm! ".a:y | endif
    endfunction
    
    nnoremap <silent> g{ :call Foo('{', 'w')<CR>
    nnoremap <silent> g} :call Foo('}', 'ge')<CR>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-21
      • 2023-02-24
      • 1970-01-01
      • 2010-11-07
      • 2011-01-07
      • 2012-01-29
      • 2021-08-11
      • 1970-01-01
      相关资源
      最近更新 更多