Vim - 如何跳转到段落的开头/结尾(在所有情况下都有效)

jim*_*mav 4 vim paragraph

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

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

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

Jor*_*nar 5

目前,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 "}").
Run Code Online (Sandbox Code Playgroud)

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

nnoremap g{ vipo<Esc>
nnoremap g} vipoo<Esc>
Run Code Online (Sandbox Code Playgroud)

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

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>
Run Code Online (Sandbox Code Playgroud)