简体   繁体   English

如何在 Vim 中启用自动折叠?

[英]How do I enable automatic folds in Vim?

How do I enable automatic folding in Vim?如何在 Vim 中启用自动折叠? set foldmethod=syntax doesn't seem to do much of anything. set foldmethod=syntax似乎没有做太多事情。

To allow folds based on syntax add something like the following to your .vimrc :要允许基于语法的折叠,请在.vimrc添加如下内容:

set foldmethod=syntax
set foldlevelstart=1

let javaScript_fold=1         " JavaScript
let perl_fold=1               " Perl
let php_folding=1             " PHP
let r_syntax_folding=1        " R
let ruby_fold=1               " Ruby
let sh_fold_enabled=1         " sh
let vimsyn_folding='af'       " Vim script
let xml_syntax_folding=1      " XML

Syntax based folding is defined in the syntax files of the language which are located in $VIM/syntax or /usr/share/vim/vimXX/syntax/ .基于语法的折叠在位于$VIM/syntax/usr/share/vim/vimXX/syntax/的语言的语法文件中定义。 But some languages do not have folding rules built into their syntax files;但是有些语言的语法文件中没有内置折叠规则; for example Python.例如Python。 For those languages you need to download something from http://vim.sf.net that does folds.对于那些语言,你需要从http://vim.sf.net下载一些可以折叠的东西。 Otherwise you will need to use folds based on indents.否则,您将需要使用基于缩进的折叠。 To do this effectively you will likely want to add the following to your .vimrc file:为了有效地做到这一点,您可能需要将以下内容添加到您的.vimrc文件中:

set foldmethod=indent
set foldnestmax=2

Other kinds of folding其他类型的折叠

There are 6 types of folds:有6种折叠类型:

manual          manually define folds
indent          more indent means a higher fold level
expr            specify an expression to define folds
syntax          folds defined by syntax highlighting
diff            folds for unchanged text
marker          folds defined by markers in the text

Personally, I only use syntax folds.就个人而言,我只使用语法折叠。 Usually, I just want to fold the method and not fold every indent level.通常,我只想折叠方法而不是折叠每个缩进级别。 Inconsistent indenting and weirdly formatted legacy code at work often makes indent folding difficult or impossible.工作中不一致的缩进和格式怪异的遗留代码通常会使缩进折叠变得困难或不可能。 Adding marks to the document is tedious and people who do not use Vim will not maintain them when they edit the document.在文档中添加标记很乏味,不使用 Vim 的人在编辑文档时不会维护它们。 Manual folds work great until someone edits your code in source control and all your folds are now in the wrong place.手动折叠效果很好,直到有人在源代码管理中编辑您的代码并且您的所有折叠现在都在错误的位置。

More reading更多阅读

  1. See :help fold-methods to learn the details of different fold methods.请参阅:help fold-methods以了解不同折叠方法的详细信息。
  2. See :help folding to learn the keyboard commands for manipulate folds.请参阅:help folding以了解用于操作折叠的键盘命令。
  3. See :help folds for help on the entire topic of folding.有关:help folds的整个主题的帮助,请参阅:help folds

JavaScript folding didn't work for me either. JavaScript 折叠对我也不起作用。 I found out when I did set syntax=javaScript (with a capital S), it suddenly worked.我发现当我确实设置了语法 = javaScript (带有大写的 S)时,它突然起作用了。

The way to enable folding in new versions of Vim has changed (I'm using vim 7.4).在新版本的 Vim 中启用折叠的方式已经改变(我使用的是 vim 7.4)。 Now you should create the file ~/.vim/ftplugin/javascript.vim (on linux) and add your code folding instructions as explained in Eric Johnson's answer.现在您应该创建文件~/.vim/ftplugin/javascript.vim (在 linux 上)并添加您的代码折叠指令,如 Eric Johnson 的回答中所述。 File type detection and loading plugins for specific file types must be enabled by putting this into your .vimrc :必须通过将其放入您的.vimrc来启用特定文件类型的文件类型检测和加载插件:

filetype plugin on

在这里尝试了所有解决方案,但没有一个适用于 NeoVim v0.3.1,直到我发现vim-javascript插件和折叠开始工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM