Ora

How do I turn off auto format in PyCharm?

Published in Code Formatting Control 3 mins read

To control PyCharm's auto-formatting and prevent it from affecting specific files or directories, you can configure exclusions within your Code Style settings. This allows you to selectively disable formatting for parts of your project without turning off the feature entirely.

Controlling Auto-Formatting for Specific Files and Directories in PyCharm

PyCharm provides a straightforward way to manage code formatting by allowing you to specify files and folders that should be ignored by the auto-formatter. This is particularly useful for generated code, third-party libraries, or files where custom formatting is essential.

Steps to Exclude Files from Auto-Formatting

Follow these steps to configure PyCharm to skip auto-formatting for designated files and directories:

  1. Open Settings/Preferences:
    • Access the PyCharm Settings dialog. You can typically do this by pressing Ctrl + Alt + S (on Windows/Linux) or Cmd + , (on macOS).
  2. Navigate to Code Style:
    • In the left sidebar of the Settings dialog, navigate to Editor | Code Style. This section manages all code formatting preferences.
  3. Specify Exclusions in the Formatter Tab:
    • Within the Code Style settings, switch to the Formatter tab.
    • Locate the "Do not format" field. In this field, you will enter the paths to files and directories you wish to exclude from automatic formatting.
    • You will use glob patterns to define these exclusions, allowing for flexible and powerful path matching.

Understanding Glob Patterns for Exclusions

Glob patterns are special characters that help you select multiple files or directories based on specific criteria. Here's a quick guide to using them effectively:

Pattern Description Example Match
*.py Excludes all Python files directly in the project root. main.py, script.py
my_module/*.js Excludes all JavaScript files within the my_module directory. my_module/api.js, my_module/ui.js
**/temp_files/ Excludes any directory named temp_files wherever it appears in the project. src/temp_files/, data/temp_files/
vendor/** Excludes an entire vendor directory and all its subcontents. vendor/library.py, vendor/assets/image.png
tests/utils.py Excludes a specific file located at tests/utils.py. tests/utils.py

💡 Practical Tip: Always use paths relative to your project's root directory. For instance, to exclude a folder named generated_code that is directly under your project root, simply type generated_code/. For Python migration scripts that you don't want reformatted, you might use */migrations/*.py to catch them across different app directories.

  1. Apply and Close:
    • Once you've entered your desired exclusion patterns, click Apply to save your changes, then OK to close the Settings dialog.

By following these steps, you gain granular control over PyCharm's auto-formatting behavior, ensuring your specific files and directories remain untouched by automatic style adjustments. For more detailed information on PyCharm's code style capabilities, you can refer to the official JetBrains PyCharm documentation.