I have this really irritating problem that I do not quite understand in which my python files are ruined (appear ruined??) when I open them in one text editor or another. Spaces disappear and with them the control blocks. Obviously this has something to do with how the whitespace is saved but I don’t fully understand it or know how to control it.
I edit the files on up to three computers in multiple editors, mostly kate, nano and vim. I thought that I had them all set to tab=4 spaces with autoindent for *.py but apparently it’s not working like that.
What do I need to do to insure my files behave consistently across text editors? Some explanation of what the problem is together with specific advice for the above editors would be greatly appreciated.
Use spaces, not tabs. Spaces are always interpreted as the same width, unlike tabs. PEP 8 (the python styleguide) recommends spaces over tabs, as does the Google Python styleguide.
Thus, if you consistently use spaces only, your code will always be indented properly.
You can check your python files for inconsistent tab usage by running the code with the
-ttswitch:Once you’ve cleaned up inconsistencies, use the Unix
expandcommand to replace tabs with spaces:where
-t 4instructsexpandto replace tabs with 4 spaces.