Sublime Forum

translateTabsToSpaces and tabSize not affecting Python

#1

I have these two settings in my User File Type.sublime-options:

tabSize 4
translateTabsToSpaces false

But they don’t seem to do much good on Python files (they work fine on javscript and html files).

That is to say, I always get spaces instead of tabs, and changing the tabSize to different settings isn’t affecting the .py files at all.

EDIT: Apparently this only happens in some Python files. I’m still not sure which ones or why. If it helps, the name of one of the files is “views.py” (although a different file, also called “views.py”, seems to work fine).

0 Likes

#2

This may be caused by an option in Default File preferences:

# Set to false to disable detection of tabs vs. spaces on load detectIndentation true
Sublime might be (incorrectly?) guessing that you’re using spaces to indent.

Other than that, you might have spaces turned on in your Python file options.

Or it could just be a bug in Sublime.

0 Likes

#3

Ah yes, that did the trick. That was driving me crazy! Thanks a lot.
It actually guessed it correctly (about 2/3rds of the file is spaces, 1/3rd tabs). I just didn’t know this setting exists.

While we’re on the subject, is there any command to convert tabs to spaces in the entire file / selected regions and vice versa?

0 Likes

#4

Not that I know of. The TextButcher plugin was conceived to solve this scenario, though. I haven’t updated it in a while, but the basic operation you need is included by default if I remember correctly.

0 Likes

#5

Where can I find that plugin? (I seem to be asking that a lot lately :wink:

0 Likes

#6

bitbucket.org/guillermooo/textbutcher

It’s very basic, but it should do the trick for you.

Alternatively, if you’re familiar with Powershell, you could use PowershellUtils too to filter the region(s) and issue mkh to save the command to disk for subsequent uses.

It would look something like so:

$_ -replace "( ){4}","`t"

In both cases, be sure to keep an eye on translatedTabsToSpaces! Otherwise replacing from spaces to tabs won’t have any effect (or so did I notice in my testing). You can use yet another plugin to control this setting file by file: SublimeModelines.

Hope it helps!

0 Likes

#7

textbutcher is empty, did you synchronize your changes?

Note that you can also use sublime’s find & replace function: Ctrl+H, look for “( ){4}”, replace by “\t”

0 Likes

#8

Oops, it looks like I didn’t! Sorry about that.

Anyway, it’s just a convenient way of storing a s&r action like the one gpfsmurf 's pointed out and give it a name.

Then you open a quick panel, type this name in, press enter, and the action’s applied to all selected regions. The actions db is saved to a file, so you won’t have to type that pattern again. It can also set options, etc.

I’ll make sure to commit the code later today.

0 Likes