Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent changing indentation from tabs to spaces

I have VSCode installed and python 3.6.8 I use tabs for my indentation. But when ever I save the file, all the tabs are being converted to spaces. This might be because of the formatter I use, i.e. Black. How do I prevent the formatter from doing that(Do all your formatting except inter-changing indents with spaces)?

Thank you

like image 967
Phani Pavan Avatar asked Jun 25 '20 10:06

Phani Pavan


People also ask

What is the difference between tabs and spaces in indentation?

Tabs are the most common problem, some code editors will treat each tab indent as very small, where as others will treat it like a huge block. This leads to not all code indentation looking the same across multiple developers systems. With spaces you do not have this translation problem, a space, is always just 1 space.

How do I indent tabs in Sublime Text?

So if you’re using tab indentation, you will have to convert the imported libraries to tabs. If you’re a Sublime Text user you can add these rules to your Preferences.sublime-settings file to ensure tabs get converted to spaces and also use 2 spaces as a default indentation level:

Should you indent your code with tabs or not?

However, it’s important you use the same method as the rest of your development team (or try and get them to change ;)), consistency is the real best answer. So if that means using tabs, then so be it. But why? The way in which you indent your code may seem trivial, and for the most part it usually is.

When does indent change in MS Word?

Indent changes automatically when bullet number 10 is reached. - Microsoft Community Indent changes automatically when bullet number 10 is reached. Hi there. Have a look at the picture below... When I use bullets/numbering, the space between the numbering and the actual paragraph changes.


2 Answers

Not only 'black' but also 'autopep8' and 'yapf' will convert the tabs to spaces. And looks like both 'black', 'autopep8' and 'yapf' haven't provided args to change the format behavior of indent. And I agree with Brett Cannon, you'd better adapt to space indent.

If you really want a try of that, you can find the black.py in site-packages folder and at the lines of 1540 in black.py change the ' '(four spaces) to ' '(a tab, copy a tab, and you can choose any spaces of the tab contains). Then you can get what you want. But, I really don't recommend it.

like image 154
Steven-MSFT Avatar answered Sep 28 '22 18:09

Steven-MSFT


Both autopep8 and black are very strict. They don't just recommend spaces over tabs, but force the usage of space indentation.

If you really want to use tabs, you should use yapf, with style use_tabs=True.

like image 43
Andre Goulart Avatar answered Sep 28 '22 20:09

Andre Goulart