如何使用 Sublime Text 2 重新格式化 HTML 代码?
我有一些格式不正确的 HTML 代码,我想重新格式化。是否有一个命令可以自动重新格式化 Sublime Text 2 中的 HTML 代码,使其看起来更好并且更易于阅读?
I've got some poorly-formatted HTML code that I'd like to reformat. Is there a command that will automatically reformat HTML code in Sublime Text 2 so it looks better and is easier to read?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
您不需要任何插件来执行此操作。
只需选择所有行 (CTRL+A),然后从菜单中选择“编辑”→“行”→“重新缩进”。
如果您的文件使用包含
.html
或.php
等 HTML 的扩展名保存,则此功能将起作用。如果您经常这样做,您可能会发现此键映射很有用:
如果您的文件未保存(例如,您刚刚将片段粘贴到新窗口),您可以通过选择菜单“视图”→“语法”→ 手动设置缩进语言在选择重新缩进选项之前
选择的语言
。You don't need any plugins to do this.
Just select all lines (CTRL+A) and then from the menu select Edit → Line → Reindent.
This will work if your file is saved with an extension that contains HTML like
.html
or.php
.If you do this often, you may find this key mapping useful:
If your file is not saved (e.g. you just pasted in a snippet to a new window), you can manually set the language for indentation by selecting the menu View → Syntax →
language of choice
before selecting the reindent option.在 Sublime 中格式化 HTML 的方法有六种左右。我已经测试了每个最流行的插件(请参阅我在博客上所做的文章< /a> 了解完整详细信息),但这里是一些最流行选项的快速概述:
Reindent 命令
优点:
缺点:
块的格式不正确
Tag
优点:
项 缺点:
块
HTMLTidy
优点:
缺点:
HTMLBeautify
优点:
缺点:
HTML-CSS-JS Prettify
优点:
缺点:
哪一个最好?
HTML-CSS-JS Prettify 是我书中的获胜者。很多很棒的功能,没什么可抱怨的。
There are half a dozen or so ways to format HTML in Sublime. I've tested each of the most popular plugins (see the writeup I did on my blog for full details), but here's a quick overview of some of the most popular options:
Reindent command
Pros:
Cons:
<script>
blocksTag
Pros:
Cons:
<script>
blocks correctlyHTMLTidy
Pros:
Cons:
HTMLBeautify
Pros:
Cons:
HTML-CSS-JS Prettify
Pros:
Cons:
Which is best?
HTML-CSS-JS Prettify is the winner in my book. Lots of great features, not much to complain about.
我能找到的唯一包是 Tag。
您可以使用包控件来安装它。 https://sublime.wbond.net
安装包控件后。转到包控制(首选项 -> 包控制),然后键入
install
,按 enter。然后输入tag
并按 enter。安装 Tag 后,突出显示文本并按快捷键 Ctrl+Alt+F。
The only package I've been able to find is Tag.
You can install it using the package control. https://sublime.wbond.net
After installing package control. Go to package control (Preferences -> Package Control) then type
install
, hit enter. Then typetag
and hit enter.After installing Tag, highlight the text and press the shortcut Ctrl+Alt+F.
我推荐这个插件:HTML/CSS/JS Prettify,它确实有效。
安装后,只需选择代码并按CTRL+SHIFT+H即可。
完毕!
I recommend this plugin: HTML/CSS/JS Prettify, It really works.
After the installation, just select the code and press CTRL+SHIFT+H.
Done!
只是一般提示。我自动整理 HTML 的方法是安装 HTML_Tidy 包,然后将以下键绑定添加到默认设置(我使用的):
这会在每次输入时运行 HTML Tidy。这可能有缺点,我自己对 Sublime 还很陌生,但它似乎做了我想要的:)
Just a general tip. What I did to auto-tidy up my HTML, was install the package HTML_Tidy, and then add the following keybinding to the default settings (which I use):
this runs HTML Tidy with every enter. There may be drawbacks to this, I'm quite new to Sublime myself, but it seems to do what I want :)
尽管问题是针对 HTML 的,但我还想提供有关如何为 Sublime Text 2 自动格式化 Javascript 代码的信息;
您可以选择所有代码 (CTRL+A) 并使用应用内功能重新缩进(
Edit
->Line
->Reindent
),或者如果您想对如何将代码格式化为更多可自定义的设置,您可以使用 Sublime Text 2 的 JsFormat 格式化插件除了Sublime Text 的默认制表符/缩进设置。https://github.com/jdc0589/JsFormat
您可以轻松安装JsFormat使用包控制(
首选项
->包控制
)打开包控制,然后键入 install,按 ENTER。然后输入js format
并按 ENTER,就完成了。(包控制器将在
Sublime
的左下栏显示安装成功和错误的状态)将以下行添加到您的键绑定中(
Preferences
->按键绑定用户
)我正在使用CTRL+ALT+2,您可以随意更改此快捷键想要。到目前为止,
JsFormat
是一个不错的插件,值得尝试!希望这会对某人有所帮助。
Altough the question is for HTML, I would also additionally like to give info about how to auto-format your Javascript code for Sublime Text 2;
You can select all your code(CTRL+A) and use the in-app functionality, reindent(
Edit
->Line
->Reindent
) or you can use JsFormat formatting plugin forSublime Text 2
if you would like to have more customizable settings on how to format your code to addition to the Sublime Text's default tab/indent settings.https://github.com/jdc0589/JsFormat
You can easily install JsFormat with using Package Control (
Preferences
->Package Control
) Open package control then type install, hit ENTER. Then typejs format
and hit ENTER, you're done.(The package controller will show the status of the installation with success and errors on the bottom left bar of
Sublime
)Add the following line to your key bindings (
Preferences
->Key Bindings User
)I'm using CTRL+ALT+2, you can change this shortcut key whatever you want to. So far,
JsFormat
is a good plugin, worth to try it!Hope this will help someone.
对我来说,
HTML Prettify
解决方案非常简单。我访问了 HTML Prettify 页面。Sublime Package Manager
按照prettify
HTML prettify
选择。完毕。看起来很棒
For me, the
HTML Prettify
solution was extremely simple. I went to the HTML Prettify page.Sublime Package Manager
prettify
HTML prettify
selection in the menuBoom. Done. Looks great
有一个名为 SublimeHtmlTidy 的插件,效果非常好
https://github.com/welovewordpress/SublimeHtmlTidy
There's a plugin called SublimeHtmlTidy which works pretty well
https://github.com/welovewordpress/SublimeHtmlTidy
只需转到
编辑 ->标签->文档上的自动格式标记
Simply go to
Edit -> Tag -> Auto-format tags on document
我认为这就是您正在寻找的:
https://github.com/victorporof/Sublime-HTMLPrettify< /a>
I think this is what you're looking for:
https://github.com/victorporof/Sublime-HTMLPrettify
我创建了一个名为 HTMLBeautify 的包,它在重新格式化 HTML 方面做得很好。我将它基于我在 1997 年发现的 Perl 脚本 - 我对其进行了更新以适用于所有新的现代标签。 :)
看看并让我知道你的想法!
https://github.com/rareyman/HTMLBeautify
I created a Package called HTMLBeautify that does a decent job of reformatting HTML. I based it off of a Perl script I found back in 1997—I updated it to work with all the new fangled modern tags. :)
Check it out and let me know what you think!
https://github.com/rareyman/HTMLBeautify
我还没有发表评论的权限,所以这只是与@peter的答案上述答案相关的附加信息。
我发现如果 IE 条件注释 中的 HTML 未按预期对齐标头不完全内联,例如向左齐平:
I am yet to have the privilege to comment so this is simply additional information related to @peter's answer above answer.
I found HTML did not align as expected if IE conditional comments in the header were not completely in-line e.g. flush to the left:
有一个很好的开源 CodeFormatter 插件,它(连同重新缩进)可以美化脏代码,甚至可以美化所有脏代码它是单行的。
There is a nice open source CodeFormatter plugin, which(along reindenting) can beautify dirty code even all of it is in single line.
我使用 tidy 和自定义构建系统来美化 HTML。
我的 Packages/User/ 目录中有 HTMLTidy.sublime-build :
同一目录中有 tidy_config.cfg 文件:
只需选择构建系统并按 CTRL+B 或CMD+B 重新格式化文件内容。一个小问题是 ST2 不会自动重新加载文件,因此要查看结果,您必须切换到其他文件并返回(或切换到其他应用程序并返回)。
在 Mac 上,我使用 macports 来安装 tidy,在 Windows 上,您必须自己下载它并在构建系统中指定 tidy 所在的工作目录:
或将其添加到 PATH 中。
I'm using tidy together with custom build system to prettify HTML.
I have HTMLTidy.sublime-build in my Packages/User/ directory:
and tidy_config.cfg file in the same directory:
And just select build system and press CTRL+B or CMD+B to reformat file content. One minor issue with that is that ST2 does not automatically reload the file so to see the results you have to switch to some other file and back (or to other application and back).
On Mac I've used macports to install tidy, on Windows you'd have to download it yourself and specify working directory in the build system, where tidy is located:
or add it to the PATH.
您可以轻松设置快捷键F12!!!
请参阅此处详细信息。
you can set shortcut key F12 easy!!!
see detail here.
HTML-CSS-JS Prettify - 毫无疑问是最好的。
Enjoy。
HTML-CSS-JS Prettify - Hands down the best.
Enjoy.
考虑使用 JsPrettier,它是 更漂亮。 Prettier 是一种广泛使用和积极开发的现代代码格式化程序。除了 HTML 之外,它还支持很多其他语言。需要 JavaScript 运行时才能运行(Node.js 或 德诺)。
Consider using JsPrettier, which is a front end for Prettier. Prettier is a modern code formatter widely used and actively developed. Besides HTML, it supports a whole lot of other languages. JavaScript runtime is required for it to run (Node.js or Deno).