skip to Main Content

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, O to fold all methods and #regions, and then press Ctrl+M, P to unfold.

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

2

Answers


  1. 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.

    Login or Signup to reply.
  2. From what I understand, you want to collapse to definitions, i.e., only 1 level? Assuming you are on Windows/Linux as you use Ctrl:

    Fold Level 1

    Ctrl + K, 1
    

    Unfold all

    Ctrl + K, J
    

    You can specify depth of folding by changing the number after Ctrl+K.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search