mrjayviper
mrjayviper

Reputation: 2388

How to configure Sublime Text 3 editor settings for specific file extensions?

I want to change some editor settings that's only applicable to certain file extensions.

As a test I created 2 files with these contents (in essence overriding what I have as default):

>  cat Preferences.sublime-settings
{
    "tab_size": 2,
    "translate_tabs_to_spaces": true
}

>  cat Powershell.sublime-settings 
{
  "tab_size": 12,
    "translate_tabs_to_spaces": true
}

>  cat Ps1.sublime-settings       
{
  "tab_size": 12,
    "translate_tabs_to_spaces": true
}

I closed and reopened Sublime Text and pressing the tab key still produces 2 spaces for tabs instead of 12.

Any ideas on how to make it work? Thank you.

Upvotes: 2

Views: 444

Answers (1)

mattst
mattst

Reputation: 14020

This answer assumes you have already installed and been trying to configure the PowerShell package from Package Control.

The reason your settings are not working is because the settings' file names that you tried are wrong. The PowerShell package uses the file name PowershellSyntax.sublime-settings for its settings, that is what you need to use.

Most packages use their package name as the file name for their .sublime-settings file but clearly not all do. In this case there is a much older PowerShell package called stposh which already made use of the file name PowerShell.sublime-settings so, in all probability, the developer of the newer PowerShell package was forced to choose a different file name to avoid a conflict. Clearly the choice of PowershellSyntax.sublime-settings made setting up the package less intuitive, in my opinion the settings file name should be mentioned in the package's README.

// Save as 'PowershellSyntax.sublime-settings'
// in your Sublime Text config 'User' folder.
{
    // 8 or even just 2 is probably better to test with
    // but stick with the massive 12 if you want to. :)
    "tab_size": 12,
    "translate_tabs_to_spaces": true
}

In future if a .sublime-settings file does not work, look on the package's homepage which is always linked from Package Control. A browse through the source file names will usually quickly reveal the correct settings file name to use. If the package is already installed, you could also open its .sublime-package file, which is a zip archive, these are stored in the Installed Packages folder.

Some Extra Hints:

The PowerShell Package should automatically recognise the following file extensions .ps1, .psm1, .psd1 and use its syntax when they are opened. To force another file extension to automatically use that syntax, e.g. .ps, click on the currently active syntax name on the far right of the status bar, a context menu will be shown, hover the mouse pointer over Open all with current extension as..., and then select PowerShell from the list.

To manually instruct Sublime Text to convert tabs to spaces, or vise versa, click on where it says either Spaces: n or Tab Size: n on the status bar, doing so will open a context menu. You can then switch between spaces and tab indentation by selecting or unselecting Indent Using Spaces. Open the same context menu again and select whichever conversion is then required from the bottom 2 menu items. This last stage can also be performed by typing indentation into the Command Palette and choosing from the self-explanatory options.

Upvotes: 2

Related Questions