VSCode Extension to fix inconsistent tab issue of Python

Question:

First of all, I wonder who was the brainless genius that decided to have indentation based language, and why couldn’t he simply used semi-colons. It is so frustrating that the tabs in python files are always go out of sync, specially when you change either an editor and/or OS.

Just wondering if there is an extension in VSCode that could fix this tab inconsistency?

Asked By: Hafiz Temuri

||

Answers:

By default vscode use four spaces instead of a tab. See your configuration options:

When opening a file, editor.tabSize and editor.insertSpaces will
be detected based on the file contents.

  "editor.detectIndentation": true

Insert spaces when pressing Tab. This setting is overridden based on
the file contents when editor.detectIndentation is on.

  "editor.insertSpaces": true

The number of spaces a tab is equal to. This setting is overridden
based on the file contents when editor.detectIndentation is on.

  "editor.tabSize": 4
Answered By: 4ndt3s

In the bottom right corner of VScode there is a small information bar that contains “Spaces: 4”

If you click that you will get a window that allows you to change all indentation to tabs or spaces.

Answered By: CrnlWes

You can fix the tab inconsistency by converting all indentation to tab or spaces.
If you open the “Show All Commands” tab, ( by pressing Ctrl+Shift+P or F1 ) and search for “convert indentation”, two options will by available:

  • convert indentation to tabs
  • convert indentation to spaces

Just choose tabs if you use tabs or spaces if use spaces as your indentation method.

Answered By: Renan Lopes

I had similar issue and none of the above solution worked for me.
Then my colleague helped me and told me to start selection from end of the line – where the IDE was showing the error for indentation and then press down arrow to select till the start of the next line of the code, Press Enter after that to remove any hidden chars in between. You can do the same for the line above – where IDE is showing the error.

This worked for me in vs code

Answered By: Abdeali Chandanwala
  1. click : ctrl+shift+p or right click on the mouse and select
    ‘Command Plate’ – this will prompt a list.
    2.select from list: ‘Convert Indentation to Spaces’
Answered By: reisy

VS Code is very intuitive in this regard. Just to illustrate the way to solve it in two clicks.

First focus on the bottom right corner of your PC.

enter image description here

Here click on spaces.

Context window will open which will give us the option to convert indentations to spaces.
Click on convert indentation to spaces and you are done.

enter image description here

Also if you are using linux environment I highly recommend using autopep library.

apt install python3-autopep8
autopep8 -i /path/to/module/problem.py
Answered By: fanbyprinciple
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.