Autoconvert spaces/tabs to default setting on file load

alexrussell 9 years ago updated by myname iswithheld 4 years ago 7
I have a load of files that are throwbacks from the days of double-space indentation. I now use tabs (tab width 2).

I've set my default indentation type to tabs and set it not to autodetect indentation:
    "auto_indent": true,
    "detect_indentation": false,
    "tab_size": 2,
    "translate_tabs_to_spaces": false

It'd be great if, upon loading a file with space-based indentation, it'd detect it and convert to tabs for me. As it stands, I'm having to notice the spaces and use "Convert indentation to tabs" which is a great feature, but it'd be great to have it automatic.

Maybe I have my settings wrong. I thought detect_indentation might be key here, but it appears to simply detect the file's indentation method and override the default with that (which is actually what I would expect, so I'm not complaining there).

Hopefully that makes sense.

Yep, I totally agree. I run across this all of the time and I used to use Textmate's convert spaces to tabs all of the time.

Same same, even if I use space and no tabs, I'd like to have my files auto-convert to space indentation.

Yes, please. Same thing for pasting. If I find some code on the web and past it into a new file, it should convert spaces/tabs.


Just created a plugin that can partially resolve this issue: https://github.com/sammarks/subl-convertindentation

Currently, it works by checking to see if the tabbing is different between the current file and the global settings when the command is run. Then if there is a difference, it converts spaces to tabbing, sets the tab width, and then converts the tabs back to spaces. That's how I've always done it from the UI, so I figured why not.

There's a setting to disable the conversion upon loading a file in case you don't like that.

Great solution!! However, it makes my ST crash, especially with large files. So, uninstalled again. Still looking for a better solution.

From what I can see, I think the reason Sublime has been causing so much pain with users dealing with tabs is that it does not appear to differentiate between soft tabs and hard tabs the way that VIM does. This is a real problem. Its nice that I can first toggle the status bar to set tabs to match the original indentation of some other developer (who apparently thought an 8-char hard tab was a 'good idea'), then convert to spaces, then change back to my preferred 4-char tab with 'Convert Indentation to Space' option. But the problem with that approach is that it has to potential to cause hundreds or thousands of lines of source to be shown as changed during code check in, when I may only want to change just a few actual lines. In VIM, you can distinguish between hard tabs and soft tabs, such that my soft tab setting is 4, but the hard tab setting is left at 8 to match the original developers setting, and yet everything lines up correctly. And any new code I add all has my correct tabs-to-spaces setting using 4 actual spaces. Its really kind of a mess in a situation like this with Sublime. I'm not sure why the authors didn't carefully review how it was being done in VIM to try to achieve the same granularity of settings, so potentially millions of lines of legacy code would not look stupid with mismatched hard and soft tabs mixed all throughout (which is extremely common due to differences among developers preferred settings).


And by the way, I just spend the last 3 hours trying to use every setting possible configuration setting offered by Sublime to fix this issue, but there simply is no way to get mixed spaces and (extra long) hard tabs to align properly in Sublime without actually transforming the entire file. If I'm not mistaken, VIM uses 8-chars for a tab stop by default, which is ridiculously large by modern standards. But that default is how some JR developers mistakenly introduce hard tabs that are excessively long into the codebase. Meaning, if they hit tab to add a new source line within an if block, the 8-char hard tab will look like its aligned because it looks the same visually as a soft tab of 4 spaces that has auto-indent enabled. In VIM, I can get both new and old lines to align properly using different hard and soft tab settings, without resorting to full reformatting of the file (to flush all those stinky tabs misalign with all the other soft tab (space) indentation throughout the source files.