How do I force Sublime Text to indent two spaces per tab?

纵饮孤独 提交于 2019-11-26 10:04:07

问题


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.


回答1:


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.




回答2:


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
}



回答3:


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:




回答4:


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



回答5:


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
        }
    ]
}



回答6:


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.



来源:https://stackoverflow.com/questions/9474090/how-do-i-force-sublime-text-to-indent-two-spaces-per-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!