Sublime Forum

Tabs not 4 spaces for existing files

#1

I’m using SublimeText 3.2.2 Build 3211 on macOS Mojave.

I have :

{
	"tab_size": 4,
  	"translate_tabs_to_spaces": true,
	"color_scheme": "Packages/Color Scheme - Default/Celeste.sublime-color-scheme",
	"ignored_packages":
	[
		"Vintage"
	],
	"word_wrap": "false"
}

If I create a new PHP file the tabs are 4 spaces.
But for existing PHP files, it’s just 2 spaces. How did this happen all of a sudden ?
Is there any way to make it to 4 spaces for all existing files ?

0 Likes

#2
  • tab_size means how wide a \t char visually is.
  • translate_tabs_to_spaces is used to insert spaces when press tab key.

If your existing files are using 2-space indentation, then spaces are always spaces and fixed-width.


https://github.com/FriendsOfPHP/PHP-CS-Fixer should be able to fix most PHP coding style. Since 4-space indentation is mandatory in PSR-2, there should be lots of formatters which can fix it.

But I am not interested in introducing how to use it here… so you probably read the doc or existing tutorial written by others.


Generally in ST, to convert 2-space indentation to 4-space indentation, you can do as the following from the indentation menu in ST’s status bar.

  1. change tab width to 2
  2. convert indentation to tabs
  3. change tab width to 4
  4. convert indentation to spaces

Or, if you have Chain of Command installed, you can set a keybinding to it. (macro should be able to do the same job, but I am not familiar with it)

// convert 2-space indentation to 4-space indentation
{
    "keys": ["f10"],
    "command": "chain",
    "args": {
        "commands": [
            [ "set_setting", {"setting": "tab_size", "value": 2} ],
            [ "unexpand_tabs", {"set_translate_tabs": true} ],
            [ "set_setting", {"setting": "tab_size", "value": 4} ],
            [ "expand_tabs", {"set_translate_tabs": true} ],
        ],
    },
},
0 Likes