5

How do I fold or collapse an arbitrary selection of code in Visual Studio Code? Is this feature supported?

Of course Sublime and Atom know this for ages.

enter image description here

Qwerty
  • 29,062
  • 22
  • 108
  • 136
  • 1
    Turns out this feature is not currently supported... https://github.com/Microsoft/vscode/issues/18805 – Qwerty Apr 03 '18 at 08:32
  • Is this what you mean https://stackoverflow.com/questions/30067767/how-do-i-collapse-sections-of-code-in-visual-studio-code-for-windows ? – primef Apr 03 '18 at 08:39
  • @primef Nope, I found it while looking for an answer. Fun fact: I did copy the question and created new question from it. – Qwerty Apr 03 '18 at 08:51
  • Why did you mark the one answer as correct, when it technically doesn't answer your question. That answer does not address "arbitrary selections" which you adamantly state in the title and in the body of the question. – Mike Kormendy Dec 01 '19 at 01:02
  • @MikeKormendy I have no idea honestly. I guess it must have helped me somehow. – Qwerty Dec 06 '19 at 01:11
  • 1
    @Qwert, sure but it doesn't really answer your specific question. It solves something else completely and doesn't warrant the designation as the correct answer. – Mike Kormendy Dec 08 '19 at 03:50

3 Answers3

5

Update for v1.70

In the Insiders Build v1.70 now is the functionality and command to truly hide arbitrary lines of code. The command is

Create Manual Folding Range from Selection
editor.createFoldingRangeFromSelection

It is bound to Ctrl+K Ctrl+, by default.

And

Remove Manual Folding Ranges
editor.removeManualFoldingRanges

It is bound to Ctrl+K Ctrl+. by default.

fold selected lines demo

Mark
  • 143,421
  • 24
  • 428
  • 436
  • Nice how quickly they managed to implemented it _after_ I suggested it here. LOL :D It's not as pretty as others do it, but that's it. It works perfectly. – Qwerty Apr 06 '18 at 16:18
  • Do you know what the `keybindings.json` `key` and `command` are for syntax folding? – sunknudsen Sep 16 '19 at 16:48
  • @sunknudsen In the `Keyboard Shortcuts` search input box at the top, just type in `fold` and you will get all your commands and bindings. I don't think there are any specific syntax-aware folding commands as opposed to indentation-based folding commands - they are just the same. Your language needs to support syntax-aware folding though. – Mark Sep 17 '19 at 14:32
  • 1
    This doesn't technically answer the OPs original question re: "arbitrary selections". – Mike Kormendy Dec 01 '19 at 01:03
2

Still not supported. The proposed solutions here are not for the original question, which is to highlight any code and fold it--not to fold formal code blocks.

2

VS Code now allows arbitrary blocks of code to be marked with // #region and // #endregion to make them collapsible.

// #region Hello
console.log("Hello world")
// #endregion
Bikibird
  • 125
  • 1
  • 5