PyCharm 4.5.3 Help

Reformatting Source Code

PyCharm lets you reformat source code to meet the requirements of your code style. PyCharm will lay out spacing, indents, keywords etc. Reformatting can apply to the selected text, entire file, or entire project.

It is also possible to apply reformatting to the parts of the source code only, using the formatting markers.

Reformatting code for module or directory

  1. In the project tool window, select module or directory to which you want to apply your reformatting.
  2. From the main menu, select Code | Reformat Code or press Ctrl+Alt+L.

    Alternatively, in the project tool window, right-click the directory and from the context menu, select Reformat Code.

  3. In the Reformat Code dialog box, specify the necessary options and filters for you reformatting and click Run.

Reformatting code for the current file

  1. In the editor of the currently opened file, press Ctrl+Shift+Alt+L.

    Note that if you select Code | Reformat Code from the main menu or press Ctrl+Alt+L, PyCharm will try to reformat the source code automatically without opening the Reformat File dialog.

  2. In the Reformat File dialog, specify options for the reformatting and click Run.

Skipping a region when reformatting source code

  1. At the beginning of the region, create a line comment (Ctrl+Slash), and then manually type the marker specified in the Formatter off field of Code Style page.
  2. At the end of the region, create a line comment (Ctrl+Slash), and then manually type the marker specified in the Formatter on field of Code Style page.
  3. Perform code reformatting, as described above.

Alternatively, create a live template to surround a block of code with formatter off/on markers, see Creating and Editing Live Templates.

Example of using formatting markers

The original source code The code after reformatting
js_formatting_markers_original
When the formatting markers are disabled, the original formatting is broken:
js_formatting_markers_off

When the formatting markers are enabled, the original formatting is preserved:

js_formatting_markers_original

See Also

Last modified: 29 June 2015