如何在Visual Studio代码中折叠或折叠代码节?

支持该特性吗?


当前回答

你应该添加用户设置:

{
    "editor.showFoldingControls": "always",
    "editor.folding": true,
    "editor.foldingStrategy": "indentation", 
}

其他回答

注意:这些快捷方式只有在编辑keybindings.json时才能正常工作

我不喜欢默认的快捷方式,我想让他们工作如下:

折叠:Ctrl + Alt +] 使用Ctrl + Shift + Alt + 全部折叠:Ctrl + k然后Ctrl +] 展开:Ctrl + Alt + [ 用Ctrl + Shift + Alt + [ 全部展开:Ctrl + k然后Ctrl + [

设置方法:

打开快捷键(JSON) (Ctrl + Shift + p) 将以下代码片段添加到该文件中 已经为折叠/展开自定义键绑定?然后你需要更换它们。

    {
        "key": "ctrl+alt+]",
        "command": "editor.fold",
        "when": "editorTextFocus && foldingEnabled"
    },
    {
        "key": "ctrl+alt+[",
        "command": "editor.unfold",
        "when": "editorTextFocus && foldingEnabled"
    },
    {
        "key": "ctrl+shift+alt+]",
        "command": "editor.foldRecursively",
        "when": "editorTextFocus && foldingEnabled"
    },
    {
        "key": "ctrl+shift+alt+[",
        "command": "editor.unfoldRecursively",
        "when": "editorTextFocus && foldingEnabled"
    },
    {
        "key": "ctrl+k ctrl+[",
        "command": "editor.unfoldAll",
        "when": "editorTextFocus && foldingEnabled"
    },
    {
        "key": "ctrl+k ctrl+]",
        "command": "editor.foldAll",
        "when": "editorTextFocus && foldingEnabled"
    },

现在在1.0版本中支持折叠功能:

源代码折叠快捷键 有新的折叠动作来折叠源代码区域 它们的折叠水平。 有一些动作可以将级别1 (Ctrl+K Ctrl+1)折叠到级别5 (Ctrl+K) Ctrl + 5)。要展开,使用全部展开(Ctrl+Shift+Alt+])。 级别折叠操作不适用于包含 当前光标。

我很难找到键盘上的]按钮(挪威式布局),在我的例子中是Å按钮。(或者从退格键开始向左按两个键,向下按一个键。)

或者,如果你想删除折叠按钮,以获得额外的空间:

"editor.folding": false

(添加到您的设置中。json文件)

命令K +命令0无效。

下面是VSCode中最常用的默认键映射。你可以很容易地自定义他们与你自己的键图。CTRL + K,然后:

Fold All: CTRL + 0

Unfold All: CTRL + J

Fold Region: CTRL + [

Unfold Region: CTRL + ]

Fold Level 1: CTRL+ 1

Fold Level 2: CTRL+ 2

Fold Level 3: CTRL+ 3

Fold Level 1: CTRL+ 4