Vim - 在折叠上方添加一行到折叠

Vim - add one line above the fold to the folding

当我在 vim 中编码时,我使用 set foldmethod=syntax 折叠我的代码。 它看起来像这样:

如何将 { 上方的行添加到首屏?所以它看起来像这样:

所以这个想法是,它(总是)将折线上方的线带入折线。 我怎样才能做到这一点?

这需要自定义 fold expression (h fold-expr),独立于:

  1. foldtext <- 你想再折一行
  2. foldignore <- 你仍然想折叠 {} 无论缩进多少

在你的 vimrc 中:

" Callback: Fold level <- next line indent
function! FoldMethod(lnum)
    let l:indent = max([indent(a:lnum+1), indent(a:lnum)])
    return l:indent / &shiftwidth
endfunction

set foldmethod=expr
set foldexpr=FoldMethod(v:lnum)