本文介绍了如何在 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/syntax/usr/share/vim/vimXX/syntax/ 的语言的语法文件中定义.但是有些语言的语法文件中没有内置折叠规则;例如Python.对于那些语言,你需要从 http://vim.sf.net 下载一些可以折叠的东西.否则,您将需要使用基于缩进的折叠.为了有效地做到这一点,您可能需要将以下内容添加到您的 .vimrc 文件中:

Syntax based folding is defined in the syntax files of the language which are located in $VIM/syntax or /usr/share/vim/vimXX/syntax/. But some languages do not 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 do not use Vim will not 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-methods 以了解不同折叠方法的详细信息.
  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 中启用自动折叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:08