[英]How do I collapse an arbitrary selection of code in Visual Studio Code for Windows
[英]How do I collapse sections of code in Visual Studio Code for Windows?
如何在 Visual Studio Code 中折疊或折疊代碼段?
是否支持此功能?
折疊功能已經推出,現在從 Visual Studio Code版本 0.10.11 開始實施。 有以下可用的鍵盤快捷鍵:
Fold折疊光標處最里面的未折疊區域:
Unfold展開光標處的折疊區域:
Fold All折疊編輯器中的所有區域:
Unfold All在編輯器中展開所有區域:
參考資料: https ://code.visualstudio.com/docs/getstarted/keybindings
從 2017 年 4 月的 Visual Studio Code 版本 1.12.0 開始,請參閱文檔中的 基本編輯 > 折疊部分。
默認鍵是:
全部折疊: CTRL + K , CTRL + 0 (零)
折疊級別 [n]: CTRL + K , CTRL + [n] *
展開全部: CTRL + K , CTRL + J
折疊區域: CTRL + K , CTRL + [
展開區域: CTRL + K , CTRL + ]
*折疊級別:折疊除最外層之外的所有類,嘗試CTRL + K , CTRL + 1
Mac:使用⌘而不是CTRL (感謝 Prajeet)
有關命令和演示,請參閱https://stackoverflow.com/a/72954133/836330 。
Fold Selected Lines
, editor.foldSelected
。 Ctrl + K Ctrl + 。
v1.17 已提供按區域折疊代碼。 按區域折疊文檔。 還有v1.19和1.23 。
[通常您可以在 //region 和 //endregion 中添加空格,例如// region and // endregion
//region and //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 + Space以查看任何語言的區域標記。
您應該添加用戶設置:
{
"editor.showFoldingControls": "always",
"editor.folding": true,
"editor.foldingStrategy": "indentation",
}
ctrl + k + 0 :折疊所有級別(命名空間、類、方法、塊)
ctrl + k + 1 :命名空間
ctrl + k + 2 :類
ctrl + k + 3 :方法
ctrl + k + 4 :塊
ctrl + k + [或] :當前光標塊
ctrl + k + j :展開
折疊/擴展的默認快捷方式是:
Ctrl + Shift + [ :“折疊”
Ctrl + Shift + Alt + [ :“全部折疊”
Ctrl + Shift + ] :“展開”
Ctrl + Shift + Alt + ] :“全部展開”
或者轉到 keybindings.json 並根據需要進行更改。
例如:
{
"key": "cmd+k cmd+m",
"command": "editor.foldAll",
"when": "editorFocus"
},
{
"key": "cmd+m cmd+k",
"command": "editor.unfoldAll",
"when": "editorFocus"
},
如果所有快捷方式都不起作用(就像我一樣),作為一種解決方法,您還可以打開命令面板( Ctrl + 3或 View -> Command Palette...)並輸入fold all
:
1.0 版現在支持折疊:
源代碼折疊快捷方式
有新的折疊操作可以根據折疊級別折疊源代碼區域。
有將級別 1 ( Ctrl + K Ctrl + 1 ) 折疊到級別 5 ( Ctrl + K Ctrl + 5 ) 的操作。 要展開,請使用全部展開 ( Ctrl + Shift + Alt + ] )。
級別折疊操作不適用於包含當前光標的區域。
我在鍵盤上找不到]
按鈕(挪威布局),在我的情況下是Å
按鈕。 (或者從退格按鈕開始,向左和向下兩個按鈕。)
使用 JavaScript:
//#region REGION_NAME
...code here
//#endregion
自Visual Studio Code 1.17起,現在支持此功能。 要折疊/折疊您的代碼塊,只需添加區域標簽,例如//#region my block name
和//#endregion
如果使用 TypeScript/JavaScript 編碼)。
例子:
這是用於折疊和展開代碼的最新內置(默認)鍵盤快捷鍵
Ctrl+Shift+[ Fold (collapse) region
Ctrl+Shift+] Unfold (uncollapse) region
Ctrl+K Ctrl+[ Fold (collapse) all subregions
Ctrl+K Ctrl+] Unfold (uncollapse) all subregions
Ctrl+K Ctrl+0 Fold (collapse) all regions
Ctrl+K Ctrl+J Unfold (uncollapse) all
注意:但在某些情況下,您的 vs 代碼擴展或用戶會更改鍵盤綁定(快捷方式)。 所以像這樣檢查的最佳選擇
例如:
全部折疊
展開全部
v1.42 對折疊的外觀和功能進行了一些很好的改進。 請參閱https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_42.md#folded-range-highlighting :
折疊范圍突出顯示
由於所有棄牌范圍的背景顏色,現在更容易發現棄牌范圍。
折疊高亮顏色主題:Dark+
該功能由設置 editor.foldingHighlight 控制,並且可以使用顏色 editor.foldBackground 自定義顏色。
"workbench.colorCustomizations": { "editor.foldBackground": "#355000" }
折疊改進
Shift + Click
折疊指示器首先只折疊內部范圍。Shift + Click
再次(當所有內部范圍都已折疊時)也將折疊父級。Shift + Click
再次展開全部。
在已經折疊的范圍上使用折疊命令 (kb(
editor.fold
))] 時,下一個展開的父范圍將被折疊。
從版本 1.3.1 (2016-07-17) 開始,Block Collapse 更加方便。
任何后跟縮進行的行都將帶有一個“-”字符以允許折疊。 如果該塊被折疊,它將被替換為“+”字符,該字符將打開折疊的塊。
( Ctrl + Shift + Alt + ] )仍然會影響所有塊,關閉一個級別。 每次重復使用都會關閉一個級別。 ( Ctrl + Shift + Alt + [ ) 以相反的方式工作。
萬歲,塊崩潰終於有用了。
這是 VS 代碼最常用的默認鍵盤映射。 並且您可以輕松地通過自己的鍵盤映射進行自定義。
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
VSCode 擴展: Fold Level ,一鍵折疊到你想要的關卡。
注意:這些快捷方式只有在您編輯 keybindings.json 時才能按預期工作
我對默認快捷方式不滿意,我希望它們按以下方式工作:
要設置它:
Preferences: Open Keyboard Shortcuts (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
+ k
)+( ctrl
+ l
)
在 Mac 上,它是 RHS Command 鍵⌘ K ,而不是代碼折疊命令的左側。
否則,左手 Command 鍵將刪除當前行⌘ K 。
我希望 Visual Studio Code 可以處理:
#region Function Write-Log
Function Write-Log {
...
}
#endregion Function Write-Log
現在 Visual Studio Code 只是忽略它並且不會折疊它。 同時 Notepad++ 和 PowerGUI 處理得很好。
更新:我剛剛注意到 Visual Studio Code 的更新。 現在支持這個!
或者,如果您想移除折疊按鈕,以獲得額外空間:
"editor.folding": false
(添加到您的settings.json
文件)
在Windows構建中,快捷方式設置為Alt + 0
命令 K + 命令 0 不起作用。
此處提供更多信息: 區域擴展https://marketplace.visualstudio.com/items?itemName=maptz.regionfolder
安裝擴展並使用 python 后,它的工作原理如下:
# region ARBITRARY_REGION_NAME
code goes here...
# endregion
此外,選擇所需區域,使用 Ctrl+M+Ctrl+R。
(即:先按住Ctrl,再按m,讓go m,再按r,讓go全部)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.