【问题标题】:Fold function in vimvim中的折叠功能
【发布时间】:2011-01-22 16:54:29
【问题描述】:

是否有任何方法或工具可以在 vim 中折叠功能,例如 Visual Studio 或 Eclipse?

【问题讨论】:

  • 如果您的意思是“自动折叠类和函数”,那么您应该在问题中明确说明。
  • 不是自动的,用户可以选择他要折叠的部分
  • vim 有很好的文档。试试 :help fold
  • 另见 :help fold-method
  • 根据语言的不同,有些插件可以提供比 vim 原生使用 &fold == syntaxindent 提出的更好的结果。

标签: vim folding


【解决方案1】:

是的,它绑定到 'z' 键,例如zO 打开所有折叠。更多信息参见 vim 中的 ":help fold"。您可以根据非常简单的规则(例如缩进)或根据代码语法进行折叠。

【讨论】:

    【解决方案2】:
        Vim folding commands
    ---------------------------------
    zf#j creates a fold from the cursor down # lines.
    zf/ string creates a fold from the cursor to string .
    zj moves the cursor to the next fold.
    zk moves the cursor to the previous fold.
    za toggle a fold at the cursor.
    zo opens a fold at the cursor.
    zO opens all folds at the cursor.
    zc closes a fold under cursor. 
    zm increases the foldlevel by one.
    zM closes all open folds.
    zr decreases the foldlevel by one.
    zR decreases the foldlevel to zero -- all folds will be open.
    zd deletes the fold at the cursor.
    zE deletes all folds.
    [z move to start of open fold.
    ]z move to end of open fold.
    

    来源:vim 文档。

    【讨论】:

    • zc 关闭光标下的折叠。 (警告:通过实验发现)
    • za 将切换折叠(如果当前关闭则打开,反之亦然)。我觉得这很方便;几乎从不使用 zc 或 zo。 vim.wikia.com/wiki/Folding#Opening_and_closing_folds
    • zfgg 将所有行从顶部折叠到光标处。 zfG 将所有行从光标折叠到底部。
    • 您也可以在视觉线模式下选择多条线(Shift+v)并用zf折叠它们。
    • 我使用 zf% 有效地折叠了一个 C 函数(我没有知道如何自动折叠的语法插件)
    【解决方案3】:
    :set foldmethod=syntax
    

    如果你有你的语言的语法文件,应该自动折叠所有函数和其他块。

    【讨论】:

    • 这应该是被接受的答案。你改变了我的生活
    【解决方案4】:

    Vim 具有出色的折叠支持。 vim 帮助系统中有很好的文档。只需打开 vim 就可以了

    :help usr_28.txt 
    

    看完后你也可以阅读

    :help folding
    

    了解更多信息。

    【讨论】:

      【解决方案5】:

      是的。 VIM 具有出色的折叠功能。我不喜欢学习太多的控件,我更喜欢自动化,所以这里是我个人使用的:

      在我的 .vimrc 中:

      set foldmethod=indent
      set foldlevel=1
      set foldclose=all
      

      这会根据缩进自动折叠您打开的文件,以缩进超过 1 级的所有内容。 foldclose 选项使折叠在我导航出折叠后自动重新关闭。

      文件内控制:

      zo - opens folds
      zc - closes fold
      zm - increases auto fold depth
      zr - reduces auto fold depth
      

      如果您对褶皱感到恼火,请使用

      : set foldmethod=syntax
      

      或按:

      zR
      

      让它们全部消失。

      【讨论】:

      • 我觉得拥有:set nofoldenable set foldlevel=99 也很有用。它在打开文件时禁用自动折叠并仅折叠外部功能(不介入并折叠“fors”和“ifs”等等)根据您的回答制作了我的配置,谢谢。
      • @merinoff 虽然它可能永远不会重要,但根据文档(对于 8.1 版),foldlevel 大于 20 的设置没有影响,因为这是内部上限。
      • 在为很多文件类型尝试了很多插件之后,这是我发现的最好的。干净,简单,没有插件。非常感谢。
      • 你不知道我看到这个解决方案时的兴奋程度。
      猜你喜欢
      • 2014-05-18
      • 2023-04-02
      • 2011-07-01
      • 1970-01-01
      • 2012-06-13
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多