Sublime Forum

Indent_to_bracket using spaces instead of TABs

#1

I use tabs for everything, and when “indent_to_bracket” is set to true, it will use SPACE instead of TABs

I can’t figure out a way to solve this. (not the best language for an image, but it happens with all languages)

0 Likes

#2

Since tabs have different behavior on different machines/editors, there is a compatibility issue for indent_to_bracket with tabs?

0 Likes

#3

I just believe that ST is using hard-coded whitespace instead of asking the view the preferred indentation method

0 Likes

#4

Which makes sense, because tabs can only specify the “level of indentation” but can not properly do visual indentation like you intend to. Spaces are exactly 1 character wide; tabs have a variable (visual) width.

You either have to

  • accept that ST does this (and that you have mixed indentation, but at least the best any only acceptable kind and of mixed indentation),
  • switch to use spaces everywhere when you visual indentation (which PEP8 suggests anyway) , or
  • not use visual indentation at all (and disable indent_to_bracket).
1 Like

#5

Or fix it yourself (when possible…), which totally makes sense If I want to use tabs instead of spaces.

Now that I know where the problem is, I came with this, which kinda works for me, thanks!

https://dl.dropboxusercontent.com/u/9303546/SublimeText/bugs/SpacesToTabs.py

0 Likes

#6

… in which case you just don’t use visual indentation.

0 Likes

#7

I don’t want to discuss tabs vs spaces. :stuck_out_tongue:
I just use tabs, for me is a visual thing, if not I was not enabling indent_to_bracket, even if shifted to a side, I just simply don’t care.

0 Likes

#8

Did you mean you prefer the following screenshot?

0 Likes

#9

Yes, I prefer that than to mixed tabs and spaces.

0 Likes

#10

Reviving old topic. The link to tito’s SpacesToTabs.py is not working any more and while I’m sure I can look for or make a plugin or complicated key-binding I’d rather ask if this could be “fixed” in Sublime Text itself, especially since this issue only seems to affect the first newly indented line.

Tabs vs spaces aside, on a particular project I do not get to dictate the style and the style chosen is based on the linux kernel, so 8-space tabbed indentation it is. Tabs may behave differently based on your tab width, but that’s the point and that’s why your editor needs to be configured to match the coding style.

0 Likes

#11

While carefully reformatting some code by hand to satisfy checkpatch.pl and finding myself spending more time replacing spaces with tabs than anything else, I finally decided to make a tiny plugin for this. So, for posterity, the following is now just floating in my User directory.


tabbed_newline.py

import sublime
import sublime_plugin


class TabbedNewlineCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        settings = self.view.settings()
        self.view.run_command("insert", {"characters": "\n"})

        if settings.get('translate_tabs_to_spaces'):
            return
        if not settings.get('auto_indent'):
            return
        if not settings.get('indent_to_bracket'):
            return

        regions = self.view.sel()
        for r in regions:
            liner = sublime.Region(self.view.line(r).begin(), r.begin())
            line = self.view.substr(liner)
            line = line.replace(' ' * settings.get('tab_size'), '\t')
            self.view.replace(edit, liner, line)

tabbed_newline.sublime-commands

[
  {
    "command": "tabbed_newline",
    "caption": "Tabbed Newline"
  },
]

This is not strictly necessary.


Key binding

{ "keys": ["enter"], "command": "tabbed_newline", "context": [
        { "key": "selector", "operator": "equal", "operand": "source", "match_all": true},
        { "key": "panel_has_focus", "operator": "equal", "operand": false },
        { "key": "setting.is_widget", "operator": "equal", "operand": false }
    ],
},

The default shift+enter is still set to the default which is the same as the default for enter, so it is a nice-to-have fallback for whenever the plugin is not doing the right thing.

0 Likes