如何在Vim中启用自动折叠? [英] How do I enable automatic folds in Vim?

查看:717
本文介绍了如何在Vim中启用自动折叠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Vim中启用自动折叠? set foldmethod = syntax 似乎没有做任何事情。

How do I enable automatic folding in Vim? set foldmethod=syntax doesn't seem to do much of anything.

推荐答案

要允许基于语法的折叠,请在 .vimrc

To allow folds based on syntax add something like the following to your .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

基于语法的折叠在语言的语法文件中定义,该文件位于$ VIM /语法中。但是有些语言的语法文件中没有内置折叠规则。例如Python。对于这些语言,您需要从 http://vim.sf.net 下载折叠的内容。否则,您将需要使用基于缩进的折叠。要有效地执行此操作,您可能需要将以下内容添加到.vimrc文件中:

Syntax based folding is defined in the language's syntax files which are located in $VIM/syntax. But some languages don't have folding rules built into their syntax files. For example Python. For those languages you need to download something from http://vim.sf.net that does folds. 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:

set foldmethod=indent
set foldnestmax=2



其他种类的折叠



有6种类型的折叠:

Other kinds of folding

There are 6 types of folds:

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

我个人只使用语法折叠。通常我只想折叠方法而不是折叠每个缩进级别。在工作中不一致的缩进和奇怪格式化的遗留代码经常使缩进折叠变得困难或不可能。在文档中添加标记很繁琐,不使用Vim的人在编辑文档时不会对它们进行维护。手动折叠很有效,直到有人在源代码管理中编辑您的代码,并且您的所有折叠现在都在错误的位置。

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 don't use Vim won't maintain them when they edit the document. Manual folds work great until someone edits your code in source control and all your folds are now in the wrong place.


  1. 参见:help fold-方法了解不同折叠方法的详细信息。

  2. 请参阅:help折叠以了解键盘命令用于操纵折叠。

  3. 请参阅:help folds 以获取有关整个折叠主题的帮助。

  1. See :help fold-methods to learn the details of different fold methods.
  2. See :help folding to learn the keyboard commands for manipulate folds.
  3. See :help folds for help on the entire topic of folding.

这篇关于如何在Vim中启用自动折叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆