Skip to end of metadata
Go to start of metadata

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:

Folding outline and toggles

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.

Viewing hidden contents

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:

Folding logical code blocks

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 .

Folding arbitrary fragments of code

Suppose, you want to hide several statements, which do not comprise a logical code block. How will you act in this case?

  1. Select the code fragment you want to hide:

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

Surrounding blocks of code with line comments

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:

  1. Select the block of code to be surrounded.
  2. Do one of the following:
    • On the main menu, choose Code | Surround with
    • Press Ctrl+Alt+T
  3. From the pop-up menu, choose the desired style of comment:
  4. 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.

Other topics of this tutorial


Your Rating: Results: BrokenPartially brokenWorksStableNo problems at all 1 rates

Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.