You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS Code:如何阻止注释状态代码自动展开及折叠注释代码?

Fixing Auto-Expanding Commented Code in VS Code

Hey there! I totally get how frustrating it is when commented code in VS Code keeps auto-expanding without your permission. Let's break down the solutions step by step, based on common scenarios you might be facing.

1. Disable Auto-Expansion When Commenting Folded Code

If you notice that every time you comment a folded block of code, it automatically unfolds—this is a default VS Code behavior that's easy to turn off:

  • Open the Settings panel with Ctrl+, (Windows/Linux) or Cmd+, (Mac)
  • In the search bar, type editor.unfoldOnComment
  • Toggle the switch from true to false

From now on, commenting folded code won't trigger an auto-unfold. Problem solved for that scenario!

2. Make Comment Blocks Foldable (and Keep Them Folded)

If your commented code isn't folding at all, or keeps popping open, adjust these settings and use manual controls:

Enable Comment Folding by Default

First, ensure VS Code recognizes comment blocks as foldable:

  • In Settings, search editor.foldingStrategy and confirm it's set to auto (this is the default, but double-check if you've modified it before)
  • Search editor.autoFoldComments:
    • Set to true if you want comment blocks to auto-fold when you open a file
    • Set to false if you prefer to fold them manually as needed

Manually Fold/Unfold Comment Blocks

Even with default settings, you can take control manually:

  • Fold a comment block: Place your cursor anywhere in the comment, then press Ctrl+Shift+[ (Windows/Linux) or Cmd+Shift+[ (Mac)
  • Unfold it: Use Ctrl+Shift+] (Windows/Linux) or Cmd+Shift+] (Mac)

Custom Foldable Comment Regions (For Tricky Cases)

If VS Code doesn't automatically recognize a large comment block as foldable, use manual region markers to force it:

// #region Commented Code Block
/* 
This is a long comment section
that you want to keep folded
without auto-expansion
*/
// #endregion

These markers work across most languages and let you fully control which comment blocks are foldable.

3. Lock Folded Comment Blocks (Prevent Accidental Unfolding)

If you want to make sure a folded comment block stays folded no matter what (even if you edit nearby code), use the lock feature:

  • Right-click the small fold arrow (next to the line numbers on the left side of the editor)
  • Select Lock Folding—the arrow will turn gray, and the block won't auto-unfold
  • To unlock it later, right-click the arrow again and choose Unlock Folding

内容的提问来源于stack exchange,提问作者Nikolius Lau

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 08:47:31