Mag*_*gne 225 indentation auto-indent sublimetext2
示例:如果我有一个包含2个空格缩进的文档,并且我希望它有4个空格缩进,如何使用Sublime Text编辑器自动转换它?
Mag*_*gne 515
这是Sublime Text 2或3中的一个巧妙的技巧,用于转换文档中的缩进间距.
TL; DR:
从2个空格转换为4个空格:
确保选项卡宽度设置为2.将2空格缩进转换为制表符,切换到制表符宽度4,然后将缩进转换回空格.
详细说明:
去:
View -> Indentation
它应该是:
Indent using spaces [x]Tab width: 2选择:
Convert Indentation to Tabs然后选择:
Tab width: 4Convert Indentation to Spaces完成.
Bri*_*tle 74
我实际上发现,让我的理智更好地定义用户首选项是这样的:
"translate_tabs_to_spaces": true,
"tab_size": 2,
"indent_to_bracket": true,
"detect_indentation": false
Run Code Online (Sandbox Code Playgroud)
这detect_indentation: false一点尤为重要,因为它强制Sublime在每个文件中都遵循这些设置,而不是View -> Indentation设置.
如果您想获得想象力,您还可以通过粘贴以下内容来定义键盘快捷键以自动重新缩进代码(YMMV)Sublime -> Preferences -> Key Binding - User:
[
{ "keys": ["ctrl+i"], "command": "reindent" }
]
Run Code Online (Sandbox Code Playgroud)
并可视化空白:
"indent_guide_options": ["draw_active"],
"trim_trailing_white_space_on_save": true,
"ensure_newline_at_eof_on_save": true,
"draw_white_space": "all",
"rulers": [120],
Run Code Online (Sandbox Code Playgroud)
在我看来,我发现一个比Magne更简单的解决方案:
在mac上:
"cmd+f" => " "(two spaces) => "alt+enter" => "arrow right" => " "(two more spaces) => set tab width to 4(this can be done before or after.
Run Code Online (Sandbox Code Playgroud)
在Windows或其他平台的变化cmd+f,并alt+enter以您find和select all热键.
注意:如果代码中有多个空格,则此方法容易出现"错误".因此它不如Magne的方法安全,但它更快(至少对我而言).
虽然许多建议在转换2 - > 4空间时有效.转换4 - > 2时遇到了一些问题.
这是我最终使用的内容:
Sublime Text 3/Packages/User/to-2.sublime-macro
[
{ "args": null, "command": "select_all" },
{ "args": { "set_translate_tabs": true }, "command": "unexpand_tabs" },
{ "args": { "setting": "tab_size", "value": 1 }, "command": "set_setting" },
{ "args": { "set_translate_tabs": true }, "command": "expand_tabs" },
{ "args": { "setting": "tab_size", "value": 2 }, "command": "set_setting" }
]
Run Code Online (Sandbox Code Playgroud)
如果您发现搜索和替换速度更快,您可以使用这样的正则表达式替换:
查找(正则表达式):( (^|\G) {2}而不是"{2}", <space>{2}您可以只写两个空格.为了清楚起见,请使用此处.)
替换为4个空格,或任何你想要的,如\t.
| 归档时间: |
|
| 查看次数: |
144740 次 |
| 最近记录: |