@ariofrio (Andres Riofrio) posted a new topic, subject "Cannot properly use Sublime on files with mixed spaces and tabs". It read:

   At my workplace, we have a large codebase, some of which dates back decades, which is edited using text editors like Vim, Emacs, and our in-house text editor, all of which insert mixed tabs and spaces in their default configurations. These files show up in Sublime with **incorrect indentation** :
    
    This occurs because, in files with mixed tabs, tabs always align to multiples of 8 characters. However, in Sublime, tabs always align according to the indentation size used to nest blocks. It's possible to configure Sublime to display these files correctly, by setting the “Tab Size” setting to 8 and disabling “Indent Using Spaces”, but at the expense of essential editing conveniences:
    
      * `Enter` almost always indents the new line incorrectly, adding a tab _after_ unnecessary spaces (see screenshot below).
      * `Ctrl`+`[` and `Ctrl`+`]` increase or decrease indentation by 2 or 4 levels instead of a single level, depending on whether the “logical” indent size for the file is 4 or 2 spaces.
      * “Indent Using Spaces” must be off, or else **copying and pasting** replaces all tab characters with spaces, creating unnecessary noise in diffs. Modific's “Revert Modified Part” action should revert all changes but unless this option is turned off, it also replaces Tabs with spaces.
    
    
    
    * * *
    
    At my workplace, **it's considered important not to introduce spurious space changes so that blame tools work properly, and because large diffs are harder to inspect and review**. Since only a few of us use Sublime, we don't have the organizational capital to change every single file in our very large codebase to use spaces instead of tabs, and to prompt every engineer in the organization to configure their own text editor to avoid mixing spaces and Tabs in existing files and new files. Instead, I carefully inspect every file before committing it to make sure I didn't introduce spurious space changes, and I work around the limitations of Sublime every time I insert a new line or change the indentation of a line. Obviously, whenever possible, I make sure to use only spaces for indentation in new files.
    
    What is needed is **an option to set the “logical” indent size and a separate option to set the “physical” size of tab characters** , and appropriate handling by the indenting subroutines in Sublime. Another option can be added which determines whether new lines and indentation changes should use mixed tabs by default (which can be determined automatically by inspecting the file). This is what is done in Vim and Emacs, for example.
    
    I have used Sublime for several years and still consider it the best text editor available, which is why I bought licenses for both versions 2 and 3. However, this issue costs me time daily and causes me to seriously consider other options periodically. **Especially in large organizations, I imagine many other engineers find themselves in the same situation.** I hope this detailed report encourages and aids the developers to fix Sublime's handling of mixed spaces and tabs.

@ThomSmith replied

    I think you're going to have to bite the bullet and fix your code. Nothing good will ever come from mixed-whitespace files. Just do a giant whitespace commit and have your repo automatically reject commits with tab characters.
    
    What source control system are you on? Subversion and git each have a blame option to ignore whitespace.

To which @rwols (Raoul Wols) replied

    I agree with @ThomSmith here and you should coordinate a change in your company's style guide while simultaneously doing a giant whitespace commit. This is a people's problem, not Sublime's problem. Sooner or later the vim gurus will start complaining about the Sublime whippersnappers with their fancy variable tab lengths.
    
    Also, if on git, I know you can use a pre-commit hook to check for tabs. Distribute that to your offending coworkers after the giant whitespace commit.
    
    EDIT: Now that I think about it, I believe you can actually accomplish what you want with a Python script that triggers on insertion of a tab character and does the appropriate thing what you want.

So @ariofrio (Andres Riofrio) replied

    Let me attempt to argue that this is Sublime's problem, not a people problem.
    
    Sublime is a text editor. Every other major text editor available on Linux (Vim, Emacs) produces mixed tabs and spaces, unless the user explicitly disables this feature. Sublime doesn't handle such files correctly. Sublime is **incompatible** with some of the most widely used text editors.

@ThomSmith replied

    > Every other major text editor available on Linux (Vim, Emacs) produces mixed tabs and spaces, unless the user explicitly disables this feature.
    
    Conversely, every major code editor lets you specify standardized whitespace, and every code base _should_ have a clearly defined standard to avoid these kinds of problems.
    
    The reason that Sublime doesn't support this behavior by default is probably that it isn't useful to most developers. The problem you have is very unusual, and even if other developers have this problem, it is unlikely that the exact solution you desire would work for them. The odds of the Sublime devs spending time implementing such a niche feature over other features that may be more widely applicable are slim.
    
    However, the beauty of Sublime is its extensibility. If you really think that it's worth the effort to implement the functionality you desire (instead of just fixing the code) then it should be possible to write a package that does so. I can't imagine that this would be less work than fixing the whitespace, but that's up to you.
    
    Basically, you'd want to set indentation to tabs and width to 8, then write new implementations of the features that don't do what you want with mixed whitespace:
    
      * A command triggered by the enter key that computes and inserts the desired indentation for the next line.
      * Commands to increase and decrease whitespace.

So @ariofrio (Andres Riofrio) replied

    ThomSmith:
    
    > The problem you have is very unusual, and even if other developers have this problem, it is unlikely that the exact solution you desire would work for them.
    
    I'm not sure what other possible solutions you have in mind; I simply proposed doing what other text editors do.

@braver (K Lageveen) replied

    Can't you get your versioning tool to ignore white space changes?
    
    Also, I wouldn't be able to deal with a code base like that. The horror!

After which, @ariofrio (Andres Riofrio) added

    @ThomSmith, @rwols, thank you for encouraging me to make such a change in my company. Certainly, it would be ideal to standardize on using only spaces for indentation. However, only around 5% of engineers use a text editor that doesn't support mixed tabs and spaces, which makes pushing for a change intimidating, especially because it would require every other engineer to change their text editor configuration and would add another step for new hires. Anyone have any ideas on how to enforce indent-using-spaces automatically? We use Subversion.
    
    We have a coding standard, but it doesn't include a recommendation on tabs v. spaces and mixing them because making the “wrong” choice negatively affects so few engineers. I might try to push for this in my smaller group first (~10) instead of the whole company (>100).
    
    By the way, this problem may be unusual in startups and in web development, but I'd be surprised to find it unusual in more traditional companies.

So @ariofrio (Andres Riofrio) replied

    braver:
    
    > Can't you get your versioning tool to ignore white space changes?
    
    Yeah, that can probably be done. But it is not enough to fix the whitespace once; unless every engineer changes their text editor configuration, they will introduce mixed tabs and spaces the next time they edit a file, which won't show up correctly next time Sublime users open the file.
    
    braver:
    
    > Also, I wouldn't be able to deal with a code base like that. The horror! 
    
    Yeah, well. It's relatively minor compared to what I have heard about other code bases or working environments, and there are significant positives to working here.

To which @idleberg (Jan) replied

    ariofrio:
    
    > and to prompt every engineer in the organization to configure their own text editor to avoid mixing spaces and Tabs in existing files and new files
    
    Have you considered using EditorConfig?

@ctf0 (muah) replied

    @ariofrio here is a macro i use to fix this issue + change 2 tabs to 4, u can change the macro to ur needs
    
    > [  
    >  {  
    >  “command”: “unexpand_tabs”,  
    >  “args”: {“set_translate_tabs”: true}  
    >  },  
    >  {  
    >  “command”: “set_setting”,  
    >  “args”: {“setting”: “tab_size”, “value”: 2}  
    >  },  
    >  {  
    >  “command”: “expand_tabs”,  
    >  “args”: {“set_translate_tabs”: true}  
    >  },  
    >  {  
    >  “command”: “unexpand_tabs”,  
    >  “args”: {“set_translate_tabs”: true}  
    >  },  
    >  {  
    >  “command”: “set_setting”,  
    >  “args”: {“setting”: “tab_size”, “value”: 4}  
    >  },  
    >  {  
    >  “command”: “expand_tabs”,  
    >  “args”: {“set_translate_tabs”: true}  
    >  }  
    >  ]

Then @braver (K Lageveen) said

    ariofrio:
    
    > Anyone have any ideas on how to enforce indent-using-spaces automatically? We use Subversion.
    
    I would look into using a pre-commit hook on the server that rejects commits with improper white space. Depending on the languages you use there are linters you can set up like that.

To which @deathaxe replied

    The screenshots look as they were created using `"tab_size": 8` which is indeed the default of most legacy editors like vim and the reason for them to add normal spaces for each indention smaller than 8. So just adjust the correct tab size and code will look normal. You may need to disable automatic tab_size detection and set up a fixed value of 8.
    [code] 
        "detect_indentation": false,
        "tab_size": 8,
        
    [/code]

To which @mg979 replied

    have you tried this?
    
    https://packagecontrol.io/packages/Stupid%20Indent

Then @deathaxe said

    I'd prefer syntax-specific settings over _Stupid Indend_ to setup custom indention settings.

Then @Nicolay (Nicolay) said

    Install the EditorConfig pluging in all those editors and configure it for your project on a file extension basis.
    
    And punish (mildly) anyone who writes mixed files. They deserve it.

To which @Hobbes50 (Simon Delamarre14) replied

    ThomSmith:
    
    > The problem you have is very unusual, and even if other developers have this problem, it is unlikely that the exact solution you desire would work for them.
    
    I have exactly the same problem in my company. Have to handle code bases whose contributors mainly use and have used Emacs for years, meaning mixed tabs and spaces all around (every sequence of 8 spaces is replaced by a single tab… weird trick that was meant to save disk space at those times I suppose).
    
    For projects I contribute to actively, I always negotiate to go for the giant-whitespace-fix-commit. Which is by the way can be more painful than one think. Sure it is possible to ignore spaces changes when reviewing commits (it “just” takes the right flag on command line, in one's VCS gui generally there is an option hidden somewhere). But there is also the problem of developers who were on a topic branch forked before the big-space-fixing-commit… puzzling merge ahead.  
    Some VCS have options to allow ignoring-spaces merges, but you do not want to open that box… So a high degree of coordination between active branches is needed.
    
    For projects I contribute to occasionally, the giant-whitespace-fix-commit is just not an option… So I end up opening emacs just to fix the indentation… or handling indentation manually in Sublime, because it is otherwise such a fantastic tool that I can afford that. 
    
    But it would be dreamy if Sublime could be versatile enough to have at least a limited support of theses mixed tab-spaces files out-of-the-box. Because unfortunately they do exist in many codebases, because of Vim and Emacs default configuration, and some people have do deal with them daily.
    
    In “Indent with spaces” mode, we would need a way to decorrelate “tab size” T from “indent size” I (number of spaces per indent), and making graphical display and basic indentation features aware that an indenting tab should be treated as T spaces.

After which, @doom4 (doom4) added

    I am also in the same situation that we use mixed tabs and spaced. I can't use sublime at work, even though me and my colleagues would love it.
    
    Did anybody come up with a solution?

So @Calvin (Calvin) replied

    Cheers everybody, I've just developed a plugin for solving this issue, it's named "Smart Indent' and will show up in Package Manager soon.
    
    If you cannot wait for the easy installation, install it manually from my Github:  
    GitHub
    
    ### YKZDY/SmartIndent-sublime
    
    Allow user separately control Indent Size and Tab Size in Sublime Text. - YKZDY/SmartIndent-sublime
    
    Hope you'll enjoy it!

After which, @Nicolay (Nicolay) added

    Short term semi-solution: Use .editorconfig files, at least one in the root folder of your project, with the right plugin for each editor. Set indent size for each file type accordingly.
    
    Long term solution: NEVER, EVER mix tabs and spaces. Your workplace is in the wrong here. You can't argue against this.

After which, @doom4 (doom4) added

    Is there a solution for sublime 4? the SmartIndent-sublime doesn't work anymore


{'source': 'discourse', 'url': 'https://forum.sublimetext.com/t/cannot-properly-use-sublime-on-files-with-mixed-spaces-and-tabs/29610', 'title': 'Cannot properly use Sublime on files with mixed spaces and tabs', 'views': 6584, 'has_accepted_answer': False, 'word_count': 2315, 'like_count': 23, 'reply_count': 7, 'participant_count': 12, 'posts': 21, 'tags': '', 'author': '@ariofrio (Andres Riofrio)', 'participants': '@ariofrio (Andres Riofrio), @ThomSmith, @rwols (Raoul Wols), @braver (K Lageveen), @idleberg (Jan), @ctf0 (muah), @deathaxe, @mg979, @Nicolay (Nicolay), @Hobbes50 (Simon Delamarre14), @doom4 (doom4), @Calvin (Calvin)', 'image_url': '', 'created_at': '2017-07-07T19:15:37.890Z', 'last_posted_at': '2021-10-05T14:18:39.039Z'}