如何在 Vim 中一次性折叠整个文件到特定级别?

发布于 2024-12-05 07:57:16 字数 314 浏览 0 评论 0原文

我想在 Vim 中按一个按钮并折叠所有代码,以便只显示达到特定(和可变)缩进级别的代码。例如,当我只想查看方法名称而不是其缩进例程时,这非常有用。

Vim: 仅折叠顶层折叠”问题有一个解决方案缩进级别,但每次更改级别时都需要设置环境。

当我的光标位于缩进级别(例如级别 2)时,我希望整个文件在所有方法中都折叠到该缩进级别。

这是 Vim 中内置的吗?有谁知道有一个好的插件可以做到这一点吗?

I want to push one button in Vim and fold all the code so only code up to a specific (and variable) indent level is showing. Very useful when I want to only see method names for example and not their indented routines.

The “Vim: Fold top level folds only” question has a solution to an indent level, but it requires an environment set each time you change levels.

When my cursor is at an indent level (say level 2), I want the entire file to fold to that indent level across all methods.

Is this built into Vim somewhere? Does anyone know of a good plugin that does this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧情别恋 2024-12-12 07:57:16

将折叠配置为通过缩进定义:

:setl foldmethod=indent

并尝试以下命令:

:let &l:foldlevel = indent('.') / &shiftwidth

要快速访问此命令,请为其创建映射,如下所示:

:nnoremap <silent> <leader>z :let&l:fdl=indent('.')/&sw<cr>

Configure folding to be defined by indentation:

:setl foldmethod=indent

and try the following command:

:let &l:foldlevel = indent('.') / &shiftwidth

To quickly access this command, create a mapping for it as follows:

:nnoremap <silent> <leader>z :let&l:fdl=indent('.')/&sw<cr>
原野 2024-12-12 07:57:16

不需要插件,它内置在 Vim 中。

“foldlevel”(或更短的“fdl”)和“foldnestmax”(“fdn”)似乎就是我们正在寻找的。您只需在 .vimrc 文件中设置“foldmethod”(或更短的“fdm”)和“foldnestmax”(或“fdn”):

set foldmethod=indent foldlevelstart=2 foldnestmax=2

或较短的版本:

set fdm=indent fdls=2 fdn=2

然后您可以使用直接命令更改折叠级别:zm或zr。

No need of a plugin, it is builtin in Vim.

'foldlevel' (or shorter 'fdl') and 'foldnestmax' ('fdn') seems to be what we were looking for. You only have to set the 'foldmethod' (or shorter 'fdm') and a 'foldnestmax' (or 'fdn') in you .vimrc file:

set foldmethod=indent foldlevelstart=2 foldnestmax=2

OR the shorter version:

set fdm=indent fdls=2 fdn=2

Then you can change the fold level with direct commands: zm or zr.

深陷 2024-12-12 07:57:16

因为当foldmethod为expr时,foldnestmax不适用,所以当我遇到你的问题时,我寻找了其他东西。这是我想到的,毫无疑问可以改进:

function! <sid>CloseFoldOpens(opens_level)
    let lineno = 2
    let last = line("$")
    while lineno < last
        if foldclosed(lineno) != -1
            let lineno = foldclosedend(lineno) + 1
        elseif foldlevel(lineno) > foldlevel(lineno - 1)
        \ && foldlevel(lineno) == a:opens_level
            execute lineno."foldclose"
            let lineno = foldclosedend(lineno) + 1
        else
            let lineno = lineno + 1
        end
    endwhile
endfunction
nnoremap <silent> z1 :%foldclose<cr>
nnoremap <silent> z2 :call <sid>CloseFoldOpens(2)<cr>
nnoremap <silent> z3 :call <sid>CloseFoldOpens(3)<cr>
nnoremap <silent> z4 :call <sid>CloseFoldOpens(4)<cr>
nnoremap <silent> z5 :call <sid>CloseFoldOpens(5)<cr>

我更喜欢编号的地图,但对于您的基于当前行的缩进,大致如下:

nnoremap <silent> z. :call <sid>CloseFoldOpens(foldlevel('.'))<cr>zv

Because foldnestmax doesn't apply when foldmethod is expr, I looked for something else when I came across your question. Here is what I came up with, which doubtless can be improved:

function! <sid>CloseFoldOpens(opens_level)
    let lineno = 2
    let last = line("$")
    while lineno < last
        if foldclosed(lineno) != -1
            let lineno = foldclosedend(lineno) + 1
        elseif foldlevel(lineno) > foldlevel(lineno - 1)
        \ && foldlevel(lineno) == a:opens_level
            execute lineno."foldclose"
            let lineno = foldclosedend(lineno) + 1
        else
            let lineno = lineno + 1
        end
    endwhile
endfunction
nnoremap <silent> z1 :%foldclose<cr>
nnoremap <silent> z2 :call <sid>CloseFoldOpens(2)<cr>
nnoremap <silent> z3 :call <sid>CloseFoldOpens(3)<cr>
nnoremap <silent> z4 :call <sid>CloseFoldOpens(4)<cr>
nnoremap <silent> z5 :call <sid>CloseFoldOpens(5)<cr>

I prefer the numbered maps, but for yours based on indentation of the current line, something along these lines:

nnoremap <silent> z. :call <sid>CloseFoldOpens(foldlevel('.'))<cr>zv
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文