在Sublime Text 3中自动将缩进从2个空格转换为4个空格 [英] Automatically convert indentation from 2 to 4 spaces in Sublime Text 3

查看:102
本文介绍了在Sublime Text 3中自动将缩进从2个空格转换为4个空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Sublime Text中缩进的自动转换,有一些主题,但是我没有找到自动进行缩进的方法.

There are a few topics about auto-conversion of the indentation in Sublime Text, but I didn't find the way to do it automatically.

我下载的许多文件都有缩进2,我讨厌,所以我想将它们转换为4.我要做的是:

Many files I download have indentation of 2, which I hate, so I want to convert them to 4. What I do is:

  1. 选择标签大小:2
  2. 将缩进转换为制表符
  3. 选择标签大小:4
  4. 将缩进转换为空格

我认为不必提及每个文件的太多工作.有人建议使用 Reindent 选项,但以我的经验,它几乎永远无法正常工作.

I don't think I need to mention that it's too much work for every single file. Some people suggest Reindent option, but for my experience, it almost never works correctly.

是否有某种构建方式,或者是一个可以一步转换缩进的软件包?

Is there some build-it way, or perhaps a package to convert indentation in one step?

推荐答案

将缩进转换为个人喜好在Sublime Text中是一项繁琐的任务,正如您所观察到的,它在StackOverflow上经常被提及.实际上,四年前,我回答了一个问题,其中几乎列出了您在问题中列出的所有步骤.

Converting indentation to a personal preference can be a tedious task in Sublime Text which, as you observe, gets mentioned on StackOverflow quite regularly. In fact 4 years ago I answered a question which lists almost exactly the steps you've listed in your question.

我也发现了3个 Command Pallette reindent 命令有点气质,有时会按我认为的样子工作,有时会做(看上去是这样)一点都没有.但是没关系...

I too have found the 3 Command Pallette reindent commands to be a little temperamental, sometimes working as I think they ought to and sometimes doing (what appears to be) nothing at all. But no matter...

当然,有一种方法可以一步一步执行您列出的动作;使用宏.

There is of course a way to perform the actions you've listed in one step; use a macro.

复制下面的宏代码并将其粘贴到 User 文件夹中,并将其保存为 IndentationTo4Spaces.sublime-macro .

Copy and paste the macro code below and save it in your User folder as IndentationTo4Spaces.sublime-macro.

[
    { "command": "set_setting", "args": {"setting": "tab_size", "value": 2} },
    { "command": "unexpand_tabs", "args": {"set_translate_tabs": true} },
    { "command": "set_setting", "args": {"setting": "tab_size", "value": 4} },
    { "command": "expand_tabs", "args": {"set_translate_tabs": true} }
]

您可以从菜单 Tools->运行宏.宏->用户->IndentationTo4Spaces ,设置键绑定或将条目添加到 Command Pallette .

You could run the macro from the menu Tools --> Macros --> User --> IndentationTo4Spaces, set up a key binding, or add an entry to the Command Pallette.

// Add keys to: Packages/User/Default (Linux/OSX/Windows).sublime-keymap
{ "keys": ["ctrl+shift+y"], "command": "run_macro_file",
  "args": {"file": "res://Packages/User/IndentationTo4Spaces.sublime-macro"} },

// Add command to: Packages/User/Default.sublime-commands
{ "caption": "Convert Indentation To 4 Spaces", "command": "run_macro_file",
  "args": {"file": "res://Packages/User/IndentationTo4Spaces.sublime-macro"} },

这篇关于在Sublime Text 3中自动将缩进从2个空格转换为4个空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆