如何在Visual Studio代码中折叠或折叠代码节?
支持该特性吗?
如何在Visual Studio代码中折叠或折叠代码节?
支持该特性吗?
当前回答
这里没有技术技巧,只是简单地调整了VsCode的首选项。
我设法显示代码折叠控件总是在VsCode通过选择和搜索“折叠”。现在只需选择始终显示这些控件。这适用于我测试的Angular 8解决方案中的Typescript代码和模板的HTML。
这是用VsCode Insiders 1.37.0在Windows 10操作系统上测试的。
其他回答
注意:这些快捷方式只有在编辑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"
},
如果任何快捷键都不起作用(就像对我来说),作为一种变通方法,你也可以打开命令面板(Ctrl + 3或View ->命令面板…)并键入折叠全部:
还可以从Insiders v1.70开始查看折叠任意行代码的功能。也就是说你选择的任何线都可以折叠!
命令和演示请参见https://stackoverflow.com/a/72954133/836330。
创建手动折叠范围从选择 editor.createFoldingRangeFromSelection
这是绑定到上面的创建命令:Ctrl+K Ctrl+,
移除手动折叠架 editor.removeManualFoldingRanges
这是绑定到上面的删除命令:Ctrl+K Ctrl+。
按区域进行代码折叠是在1.17版本中实现的。按区域文档折叠。以及v1.19和1.23。
[通常你可以添加一个空格,例如//region和//endregion到//region和//endregion,它也可以工作。]
TypeScript/JavaScript: //#region and //#endregion or // #region and // #endregion
C#: #region and #endregion
C/C++: #pragma region and #pragma endregion
F#: //#region and //#endregion
PowerShell: #region and #endregion
Python: #region and #endregion
VB: #Region and #End Region
PHP: #region and #endregion
Bat: ::#region and ::#endregion or REM #region and REM #endregion
Markdown: <!-- #region --> and <!-- #endregion -->
Golang //region and //endregion or //#region and //#endregion
Java //#region and //#endregion
CSS/SCSS/Less: /* #region */ and /* #endregion */ or /*#region*/ and /*#endregion*/
SCSS/Less: // #region and // #endregion
Go: // region, // endregion and // #region, // #endregion
shellscript: # region and # endregion
Perl5 #region and #endregion or =pod and =cut
sql --#region and --#endregion
重要提示:如果你没有在列表中看到你的语言::
每种语言都有标记可用的代码片段。输入'#'并调用代码补全来查看它们。若要为您的语言配置区域标记,请与语言扩展提供商联系。
所以键入#,然后Ctrl+空格来查看任何语言的区域标记。
从2017年4月Visual Studio Code 1.12.0版本开始,请参阅文档中的基本编辑>折叠部分。
默认键为:
全部折叠:CTRL+K, CTRL+0 (0) 折叠等级[n]: CTRL+K, CTRL+[n]* 展开全部:CTRL+K, CTRL+J 折叠区域:CTRL+K, CTRL+[ 展开区域:CTRL+K, CTRL+]
*折叠级别:折叠所有,但最外层的类,尝试CTRL+K, CTRL+1
mac:使用⌘而不是CTRL(感谢Prajeet)
你应该添加用户设置:
{
"editor.showFoldingControls": "always",
"editor.folding": true,
"editor.foldingStrategy": "indentation",
}