如何强制Sublime Text每个标签缩进两个空格?

本文翻译自:How do I force Sublime Text to indent two spaces per tab?

Is there a way to force Sublime Text 2 to always indent two spaces per tab when working with Ruby files? 有没有办法强制Sublime Text 2在使用Ruby文件时始终每个标签缩进两个空格?

I know that indentation can be set under the view -> indentation menu option, but it does not stick. 我知道缩进可以在view -> indentation菜单选项下设置,但它不会粘。 Every time I open a new file and hit tab, it reverts back to four spaces. 每次我打开一个新文件并点击标签时,它都会恢复为四个空格。


#1楼

参考:https://stackoom.com/question/dkdu/如何强制Sublime-Text每个标签缩进两个空格


#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): 如果您想强制选择的选项卡设置,忽略文件中可能已经发生的事情,那么您应该在配置中包含detect_indentation (您的用户设置或语法特定设置,具体取决于您是希望全局还是每个文件类型) :

{
    "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. 您也可以使用Sublime Text 2(右侧)底栏中默认显示“Tab Size 4”的文本链接,单击该文本链接,然后在窗口中显示从1个空格设置选项卡大小的选项一直到8个空格,包括将标签转换为空格和空格到标签的选项。

Looks like this: 看起来像这样:

Sublime Text 2中的选项卡选项


#4楼

Can I suggest EditorConfig ? 我可以推荐EditorConfig吗? There is an extension to autoload and apply the .editorconfig file. 自动加载有一个扩展并应用.editorconfig文件。 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看起来像。

# 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 followed the previous answers, including adding the detect_indentation line, and my tabs were still five spaces. 我按照之前的答案,包括添加detect_indentation行,我的标签仍然是五个空格。 Then I realized that selecting Preferences -> Settings -> More -> Syntax Specific -> User from a ruby file was opening up Ruby on Rails.sublime-settings for me, not Ruby.sublime-settings . 然后我意识到从ruby文件中选择Preferences -> Settings -> More -> Syntax Specific -> User为我打开了Ruby on Rails.sublime-settings ,而不是Ruby.sublime-settings

I renamed my Ruby on Rails.sublime-settings file to Ruby.sublime-settings . 我将Ruby on Rails.sublime-settings文件重命名为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. 我继续在Ruby on Rails.sublime-settings相同的设置,只是为了确定。


#6楼

I use Stupid Indent package. 我使用Stupid Indent包。

Install Package -> Stupid Indent Install Package - > Stupid Indent

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

Copy settings (of Ruby part) into. 将设置( Ruby部分)复制到。

{
    "configuration":
    [
        {
            "patterns": ["*.rb"],
            "tab_size": 2,
            "translate_tabs_to_spaces": true
        }
    ]
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章