If at some point you consider certain lines of your code unimportant, you can hide them from view, reducing them to a single visible line, by means of the code folding. In this brief tutorial, we'll consider the basics of code folding, and the ways to fold and unfold your code:
First, have a look at the code folding outline to the left of your code. This outline is represented by a thin line that runs along the code blocks. When unfolded (expanded), it starts and ends with the folding toggles and . When a code block is folded (collapsed), the two toggles are replaced with just one: , the collapsed code is represented by the first line, and the other lines are hidden behind the three dots. To unfold the code block, click this plus sign.
Hovering your mouse pointer over an outline shows it bold:
Note that you can make the outline invisible. To do that, open the Settings dialog, and under the Editor node, click the page Code Folding. On this page, clear the check box Show code folding outline. When the code folding outline and toggles are invisible, you have to use main menu commands (Code | Folding | Expand/Collapse) or keyboard shortcuts.
Suppose, you do not want to have a block unfolded, but nevertheless would like to know what's there inside? Most easy: just hover your mouse pointer over the three dots, and you will see the code block contents in a pop-up window:
PyCharm also helps viewing contents of a code block that ran outside of the editor pane. Look at the image below: the beginning of the file is not visible, but you do not need to scroll the editor pane to view the missing contents - you can just hover your mouse pointer over the closing toggle and view the "runaway" contents in a pop-up window:
By default, the folding outline shows for the class and function bodies. If you want to hide contents of a class or a function, click one of the folding toggles or . The code collapses, and the two toggles are replaced with just one: , the collapsed code is represented by the first line, and the other lines are hidden behind the three dots. To unfold the code block, click the plus sign .
Suppose, you want to hide several statements, which do not comprise a logical code block. How will you act in this case?
- Select the code fragment you want to hide:
- Do one of the following:
- On the main menu. choose Code | Folding | Fold Selection/Remove Region
- Right-click the selection, and on the context menu, choose Folding | Fold Selection/Remove Region
- Press Ctrl+Period
The arbitrary code block gets hidden, as described above.
Note that your selection should include the complete logical structures. For example, code folding will not work, if you select the beginning of a class or function definition.
PyCharm makes it possible to surround logical code blocks with the line comments in one of the two special styles:
- Visual Studio style:
- NetBeans style:
This is how it's done:
- Select the block of code to be surrounded.
- Do one of the following:
- On the main menu, choose Code | Surround with
- Press Ctrl+Alt+T
- From the pop-up menu, choose the desired style of comment:
- If necessary, type some meaningful description of the region.
When a block of code is surrounded with such comments, the folding toggles appear to the left of the corresponding lines, thus marking the borders of a region:
To collapse the region, just click one of the toggles. The entire region hides behind the description:
Learn more about code folding here.