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?
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
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.
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.
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
- 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’
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.
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.
Also if you are using linux environment I highly recommend using autopep library.
apt install python3-autopep8
autopep8 -i /path/to/module/problem.py
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?
By default vscode use four spaces instead of a tab. See your configuration options:
When opening a file,
editor.tabSize
andeditor.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 wheneditor.detectIndentation
is on.
"editor.insertSpaces": true
The number of spaces a tab is equal to. This setting is overridden
based on the file contents wheneditor.detectIndentation
is on.
"editor.tabSize": 4
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.
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.
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
- 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’
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.
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.
Also if you are using linux environment I highly recommend using autopep library.
apt install python3-autopep8
autopep8 -i /path/to/module/problem.py