Sometimes we need to have a default support convert existing tabs to spaces automatically.

Some settings need to be changed in code stypes

PyCharm Convert Tab to Spaces automatically.

  • Open PyCharm Editor

  • Go to File and Settings

  • or Use Shortcut to Open Settings

    • MacOS: ⌘ + ;
    • Window/Linux: Ctrl + Alt + S pycharm Settings
  • Select Editor + Code Style + Python Tab

    pycharm Code Styles Editor

Change to below values

  • Tab size: 4

  • Indent: 4

  • Continuation indent: 8

  • Save the changes by clicking OK

Manually, You can do

  • Select the code file you want to apply
  • Go to Edit Menu
  • Select Convert Indents
  • It has two options
    • To Spaces
    • To Tabs
  • Select To Spaces, It indents the code into spaces.
  • This applies to the current file with space indented.

Another way of using Find Action

  • Use short shortcut code to open Find Action

    • Windows : Ctrl + Shift + A
    • MacOS : ⌘+ ⇧+ A
  • It Opens the Find Action Popup

    pycharm Find Action

  • Type To Spaces in the Search box and select it

    pycharm Find Action To Spaces Indentation

  • It indents with spaces current file

Another option using .editorconfig file

  • Create a .editorconfig file, this is a generic editorconfiguration settings file.
  • Add the below properties

.editorconfig

indent_style=space
indent_size=2

Change Indentation spaces from 4 to 2 spaces in PyCharm

By default, Pycharm uses Tabs with spaces.

To change that.

  • Go to File and Settings

  • or Use Shortcut to Open Settings

    • MacOS: ⌘ + ;
    • Window/Linux: Ctrl + Alt + S pycharm Settings
  • Select Editor + Code Style + Python Tab

    pycharm Code Styles Editor

Change to below values from 4 to 2

  • Tab size: 2
  • Indent: 2
  • Continuation indent: 4