Convert Tabs to Spaces in PyCharm
Learn how to convert tabs to spaces in PyCharm for one file or a whole project, adjust indentation settings, and verify the result.
Before you start
This process runs locally in the installed PyCharm editor. You need a project open and the file or files whose indentation you want to change. PyCharm can convert leading indentation without modifying spaces that appear inside strings or in the middle of a line.
Step-by-step instructions
To convert the current file, open it in the editor. From the main menu, choose Edit, then Convert Indents, and select To Spaces. PyCharm replaces leading tabs with spaces using the configured indent size.
To convert several files at once, select the files or directories in the Project tool window, then use the same command: Edit, Convert Indents, To Spaces. Confirm the action when PyCharm asks which files to include.
To make new indentation use spaces automatically, open Settings or Preferences, go to Editor, then Code Style. Select the relevant language and set Tab size, Indent, and Continuation indent to the same number of spaces. PyCharm will not necessarily rewrite existing files when you change this setting; it mainly affects future formatting and insertion.
Some versions label the conversion command as Convert Indents. In newer releases, you can also use the Find Action dialog: open it, enter "Convert Indents," and choose To Spaces. Menu wording may vary slightly between editions, while the underlying action remains available.
The conversion is reversible. To return indentation to tabs, choose To Tabs from the same Convert Indents command. If you want to restore a previous state exactly, use local file history or your version control system before converting.
Check the output
Enable whitespace display to verify the result visually. Open Settings or Preferences, go to Editor, General, Appearance, and turn on Show Whitespace. Tabs appear as small marks, while spaces appear as dots. You can also check a single indentation level by moving the caret to the start of the line and inspecting the visible characters.
For a stricter check, search for tab characters with the editor's regular expression search. Enter an escaped tab character in the search field and search the file or project. If the search finds tabs only inside strings or other intentional content, the indentation conversion succeeded.
Common problems and next steps
If the command does not change a file, check that the file type is recognized by the editor. Indentation conversion applies to text-based supported formats; binary or unknown file types may be skipped or unavailable.
If some lines still use tabs, review them manually. The conversion may preserve mixed indentation already present in the file, or prevent changes to read-only files. Make the file writable and rerun the command.
If the indentation looks uneven after conversion, compare the Code Style indent values with the spacing that appears in the file. A file created with a different tab size can look misaligned until its indentation is normalized. Select the file, choose Code, then Reformat Code to apply the current style.
For project-wide consistency, enable the inspection that detects tabs or spaces according to your policy. Open Settings or Preferences, go to Editor, Inspections, and locate the indentation-related check. After adjusting it, run Code, Inspect Code on the project to find remaining inconsistencies.