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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4789605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:06:04  来源:igfitidea点击:

How do I enable automatic folds in Vim?

javascriptperlvimfolding

提问by Eric Johnson

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

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

回答by Eric Johnson

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 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.netthat 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:

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

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

More reading

更多阅读

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

回答by Bet Lamed

JavaScript folding didn't work for me either. I found out when I did set syntax=javaScript(with a capital S), it suddenly worked.

JavaScript 折叠对我也不起作用。我发现当我确实设置了语法 = javaScript(带有大写的 S)时,它突然起作用了。

回答by Diego

The way to enable folding in new versions of Vim has changed (I'm using 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. File type detection and loading plugins for specific file types must be enabled by putting this into your .vimrc:

在新版本的 Vim 中启用折叠的方式已经改变(我使用的是 vim 7.4)。现在您应该创建文件~/.vim/ftplugin/javascript.vim(在 linux 上)并添加您的代码折叠指令,如 Eric Johnson 的回答中所述。必须通过将其放入以下来启用特定文件类型的文件类型检测和加载插件.vimrc

filetype plugin on

回答by geekymartian

Tried all of the solutions here and none worked with NeoVim v0.3.1 Until I found the vim-javascriptplugin and folding started to work.

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