在 Sublime Text 2 中打开时如何自动缩进 XML 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19383743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:12:56  来源:igfitidea点击:

How to indent XML files automatically when opened in Sublime Text 2?

xmlsublimetext2indentation

提问by Geert Schuring

I'm using Sublime Text 2 (with the Indent XML plugin) for editing XML files.

我正在使用 Sublime Text 2(带有 Indent XML 插件)来编辑 XML 文件。

My question is: How can I configure Sublime Text to automatically execute the "Indent XML" action right after opening a file named *.xml?

我的问题是:如何配置 Sublime Text 以在打开名为 *.xml 的文件后立即自动执行“缩进 XML”操作?

Thanks!

谢谢!

回答by Adi Sutanto

To indent / prettify / beautify XML, I use SublimeText Indentplugin:

为了缩进/美化/美化 XML,我使用SublimeText Indent插件:

  1. Install the package with Package Control (search "indentxml").
  2. Open any XML file, or create a new file and paste any XML into it.
  3. Ctrl-K, F to indent it.
  1. 使用包控制安装包(搜索“indentxml”)。
  2. 打开任何 XML 文件,或创建一个新文件并将任何 XML 粘贴到其中。
  3. Ctrl-K, F 缩进。

回答by thewheat

Try this plugin:

试试这个插件:

1) Tools > New Plugin

1) 工具 > 新插件

2) Copy and paste code below

2)复制并粘贴下面的代码

3) Save in the Packages/User directory with a .py extension (should be the default directory)

3)保存在Packages/User目录下,扩展名.py(应该是默认目录)

4) Open any XML file and it should run. [Open console (Ctrl+~ in Windows) to see any errors]

4) 打开任何 XML 文件,它应该会运行。[打开控制台(Windows 中的 Ctrl+~)以查看任何错误]

The code supports both "Indent XML" and "IndentX". If they don't exist I believe the command fails silently and shouldn't affect anything. I'm a plugin newbie but hope this helps!

该代码同时支持“Indent XML”和“IndentX”。如果它们不存在,我相信该命令会默默地失败并且不应该影响任何事情。我是插件新手,但希望这会有所帮助!

# Packages/User/AutoIndent.py
import sublime, sublime_plugin

class OnOpenCommand(sublime_plugin.EventListener):  
  def on_load(self, view):  
    if view.file_name().lower().endswith(".xml") :
      #print "{0}: Auto indenting {1} with Indent XML's auto_indent command".format(__file__, view.file_name())
      view.run_command("auto_indent")       
      #print "{0}: Auto indenting {1} with IndentX's basic_indent_tags command".format(__file__, view.file_name())
      view.run_command("basic_indent_tags")