0

I'm new to Python, and I'm trying to fold my code in a way that's similar to regular Visual Studio with C#.

In VS, you can press Ctrl+M, Ctrl+O to fold all methods and #regions, and then press Ctrl+M, Ctrl+P to unfold.

VS Code has a similar feature with Ctrl+K, Ctrl+0 and Ctrl+K, Ctrl+J. However, this feature folds a lot of the contents of the functions, like if/else statements.

EDIT: Looking at the attempted answers on the duplicate question, I think the correct answer is simply that it isn't possible to fold Python code to only the function names.

4
  • this question has been asked before, so I've closed it. unfortunately, the dup target doesn't have any correct/accurate answers (and was even wrongly closed as a duplicate). you'll need to wait for someone to give it a proper answer. Commented Mar 29, 2024 at 8:48
  • @starball The question has been asked, but as you said, it has a bunch of incorrect answers. If a person finds that thread on google like I did, they'll end up wasting time reading through a bunch of answers that don't really work. If that's the case, then I think the correct answer is "it isn't possible to do this." Commented Mar 29, 2024 at 12:14
  • 1
    fyi, I'm planning to write a correct answer to the dup-target. As part of that, I've reached out to a maintainer about the status of a related issue ticket and am waiting on their reply. Commented Mar 30, 2024 at 7:57
  • 1
    my answer has now been posted at stackoverflow.com/a/78264174/11107541 Commented Apr 2, 2024 at 21:54

1 Answer 1

0

If you are familiar with #region you can still use this tag in vscode.

You could add #region and #endregion before and after the code that needs to be folder, such as:

# region
print("HelloWorld")

#endregion

enter image description here

You can manually click on the arrows, or use the shortcuts ctr+8 and ctrl+9 to collapse or expand.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't see how this answers the question. this just adds more folding regions. the asker is looking for an efficient way to toggle folding of a specific subset of all types of folding regions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.