548

Is there a way to force Sublime Text 2 to always indent two spaces per tab when working with Ruby files?

I know that indentation can be set under the view -> indentation menu option, but it does not stick. Every time I open a new file and hit tab, it reverts back to four spaces.

Mohamad
  • 34,731
  • 32
  • 140
  • 219
  • Sublime is slightly different/weird by default. Although it may save the file with multiple spaces the editor it self deletes both spaces if it identifies it as indentation. – Lime Sep 18 '15 at 03:26

6 Answers6

977

If you want it for all files, go to Preferences -> Settings - Default/User. But as several comments below indicate, Syntax Specific settings can limit it to just the languages you choose.

To limit this configuration to Ruby files, first open up a Ruby file in the editor, and then go to Preferences -> Settings - Syntax Specific. This should open a settings window named Ruby.sublime-settings

Save these settings:

{
  "tab_size": 2,
  "translate_tabs_to_spaces": true,
  "detect_indentation": false
}

Repeat for any other syntax types by opening a file of that type and going back to the preferences to open the correct preferences file for that syntax.

I have edited this to include the "detect_indentation" line per the requests in comments; I previously used the Default/User to set my tab size, and have not needed the tab detection, but whether that is due to the global config or due to the fact that I have rarely opened files with tabs, I do not know.

Restarting should not be necessary, although in some instances it can be.

dǝɥɔS ʇoıןןƎ
  • 1,674
  • 5
  • 19
  • 42
DGM
  • 26,629
  • 7
  • 58
  • 79
  • 12
    From build 2181. You can configure the settings from Preferences -> Settings - Default/User. Best to put it in the User settings as that is the intended approach. – Kevin Jalbert Feb 27 '12 at 23:41
  • 21
    @KevinJalbert (and DGM) Actually he should put that in syntax specific settings, so it only affects Ruby files. – Paul Hoffer Feb 28 '12 at 00:18
  • 7
    Notice the value is **true** and *not* "true". – earthmeLon Jan 29 '13 at 15:27
  • 1
    This is especially useful since Sublime Text's own website documentation incorrectly suggests using camelCase instead of underscores in the setting keys... still. (http://www.sublimetext.com/docs/indentation) – Eric G Jul 19 '13 at 20:54
  • @EricGoldberg - I'm not sure how you got to that documentation page, but if you go to http://www.sublimetext.com > Support, and under Documentation, click the Sublime Text 2 link, you are brought to http://www.sublimetext.com/docs/2 - from there, you can find the docs on indentation, and the setting keys correctly use underscore: http://www.sublimetext.com/docs/2/indentation.html – jbyrd Feb 09 '15 at 04:53
  • @jbyrd I probably did a Google search. They should update older docs pages and link to version 2 / 3 of the docs, or they'll be fighting their own Google rankings for quite a while after each new release. – Eric G Feb 09 '15 at 23:28
  • I was looking for the exact opposite (4 spaces) for an existing file, this worked: https://forum.sublimetext.com/t/can-i-easily-change-all-existing-2-space-indents-to-4-space-indents/40158/2 – Selçuk Cihan Nov 14 '19 at 10:19
173

If you want to force your chosen tab setting, ignoring what's likely already going on in the file, then you should include detect_indentation in your configuration (your User settings or your Syntax Specific settings, depending on if you want it global or per-filetype):

{
    "tab_size": 2,
    "translate_tabs_to_spaces": true,
    "detect_indentation": false
}
James Chevalier
  • 10,604
  • 5
  • 48
  • 74
  • Thanks for the tip! Without `detect_indentation` turned off I was getting very strange behavior (it was detecting the indentation incorrectly and using that instead of what I had specified in my settings). – JacobEvelyn Feb 27 '14 at 16:24
  • Doing this in default settings worked for me. Make sure you look to see if the setting already exists. If you add it and it's declared later as true, it will remain set as true. – David Mar 17 '14 at 20:23
  • 3
    Be aware that changes to the Default Settings will be overwritten by Sublime Text updates, etc. Changes to the User Settings will not be overwritten. – James Chevalier Mar 17 '14 at 20:57
  • 6
    THANK YOU!! `"detect_indentation":false` is *critical* else the other settings are ignored. finally! – zzzeek May 05 '14 at 13:22
  • It is a good point but your example is inappropriately indented ;-) – snow6oy Jun 01 '16 at 17:34
103

You can also do this with the text link in the bottom bar of Sublime Text 2 ( On the right side ) that says "Tab Size 4" by default, click that and a window comes up with options to set the tab size from 1 space all the way up to 8 spaces and includes options to convert tabs to spaces and spaces to tabs.

Looks like this:

Tab Options in Sublime Text 2

Nate
  • 12,963
  • 4
  • 59
  • 80
Taskism
  • 1,087
  • 1
  • 8
  • 3
  • 5
    Great tip, but do you know if that becomes a sticky setting? For example, changing the indentation from the top menubar only applies to the current open file, and not others, which was my original problem. – Mohamad Oct 10 '13 at 14:20
  • 7
    This **does not work**. It's a good tip, but it's not sticky: closing and reopening the file (or opening another file) reverts indentation to four spaces. – Mohamad Apr 15 '14 at 15:11
  • 1
    Yeah, it is not sticky, it's per file. You have to go to into the settings of Sublime Text to setup your default setting for tabs/spaces. – Taskism May 28 '14 at 22:03
  • This resolved my issue in Sublime Text 3. Thank you! – Adrian May 07 '16 at 23:44
  • Yes, along the Status Bar, bottom right corner you will see Spaces: 2 or Spaces: 4, when you left click on this it brings up an option window to set the Indent style and Tab width. – Sal Nov 30 '22 at 07:23
36

Can I suggest EditorConfig? There is an extension to autoload and apply the .editorconfig file. Then just create one in the root of your project.

.editorconfig

[*.rb]
indent_style = tab
indent_size = 2

This way, your settings are project-specific and file-specific if you use different styles for each project or language.


Here is what my own .editorconfig looks like.

# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
Nate
  • 12,963
  • 4
  • 59
  • 80
  • 2
    I didn't want to make this a global change. So this was the only solution that worked. I did some research into EditorConfig and it looks very nice for IDEs that support it. – Neil Monroe Jul 29 '15 at 22:49
  • 1
    It just so happened that I already had an .editorconfig in my project and did not even know it. (angular-cli generates one) – uglycoyote Nov 15 '17 at 07:03
  • 1
    How is this not the top answer!? Exactly what I was looking for. Thanks! – Humberto Garza Jul 07 '21 at 13:46
  • I like the .editorconfig solution. It is configurable for each file and each project. You can have a consistent coding style across an entire team. – Sal Nov 30 '22 at 07:18
10

I use Stupid Indent package.

Install Package -> Stupid Indent

Preferences -> Package Settings -> Stupid Indent -> Setting-Users

Copy settings (of Ruby part) into.

{
    "configuration":
    [
        {
            "patterns": ["*.rb"],
            "tab_size": 2,
            "translate_tabs_to_spaces": true
        }
    ]
}
Rahn
  • 4,787
  • 4
  • 31
  • 57
1

I followed the previous answers, including adding the detect_indentation line, and my tabs were still five spaces. Then I realized that selecting Preferences -> Settings -> More -> Syntax Specific -> Userfrom a ruby file was opening up Ruby on Rails.sublime-settings for me, not Ruby.sublime-settings.

I renamed my Ruby on Rails.sublime-settings file to Ruby.sublime-settings. Finally two-space tabs worked! I went ahead and put the same settings in Ruby on Rails.sublime-settings as well, just to be sure.

Tim Koelkebeck
  • 795
  • 2
  • 9
  • 18