emacs 可以为我重新缩进一大块 HTML 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/137043/
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
Can emacs re-indent a big blob of HTML for me?
提问by raldi
When editing HTML in emacs, is there a way to automatically pretty-format a blob of markup, changing something like this:
在 emacs 中编辑 HTML 时,是否有一种方法可以自动对标记的 blob 进行漂亮格式化,并更改如下内容:
<table>
<tr>
<td>blah</td></tr></table>
...into this:
...进入这个:
<table>
<tr>
<td>
blah
</td>
</tr>
</table>
采纳答案by jfm3
By default, when you visit a .html
file in Emacs (22 or 23), it will put you in html-mode
. That is probably not what you want. You probably want nxml-mode
, which is seriously fancy. nxml-mode
seems to only come with Emacs 23, although you can download it for earlier versions of emacs from the nXML web site. There is also a Debian and Ubuntu package named nxml-mode
. You can enter nxml-mode
with:
默认情况下,当您访问.html
Emacs 中的文件(22 或 23)时,它会将您放入html-mode
. 那可能不是您想要的。你可能想要nxml-mode
,这是非常花哨的。nxml-mode
似乎只随 Emacs 23 一起提供,尽管您可以从nXML 网站下载它以用于早期版本的 emacs 。还有一个名为nxml-mode
. 您可以输入nxml-mode
:
M-x nxml-mode
You can view nxml mode documentation with:
您可以通过以下方式查看 nxml 模式文档:
C-h i g (nxml-mode) RET
All that being said, you will probably have to use something like Tidyto re-format your xhtml example. nxml-mode
will get you from
综上所述,您可能必须使用Tidy 之类的工具来重新格式化您的 xhtml 示例。 nxml-mode
会让你从
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<table>
<tr>
<td>blah</td></tr></table>
</body>
to
到
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<table>
<tr>
<td>blah</td></tr></table>
</body>
</html>
but I don't see a more general facility to do line breaks on certain xml tags as you want. Note that C-j
will insert a new line with proper indentation, so you may be able to do a quick macro or hack up a defun
that will do your tables.
但我没有看到更通用的工具可以根据需要对某些 xml 标签进行换行。请注意,这C-j
将插入一个具有适当缩进的新行,因此您可以执行快速宏或修改一个defun
可以处理表格的内容。
回答by vava
You can do sgml-pretty-print
and then indent-for-tab
on the same region/buffer, provided you are in html-mode or nxml-mode.
如果您处于 html-mode 或 nxml-mode,您可以sgml-pretty-print
然后indent-for-tab
在相同的区域/缓冲区上执行。
sgml-pretty-print
adds new lines to proper places and indent-for-tab
adds nice indentation. Together they lead to properly formatted html/xml.
sgml-pretty-print
在适当的位置添加新行并indent-for-tab
添加漂亮的缩进。它们一起导致格式正确的 html/xml。
回答by Jay
http://www.delorie.com/gnu/docs/emacs/emacs_277.html
http://www.delorie.com/gnu/docs/emacs/emacs_277.html
After selecting the region you want to fix. (To select the whole buffer use C-x h)
选择要修复的区域后。(要选择整个缓冲区,请使用 Cx h)
C-M-q
Reindent all the lines within one parenthetical grouping(indent-sexp).
C-M-\
Reindent all lines in the region (indent-region).
CMq
重新缩进一个括号内的所有行(indent-sexp)。
厘米-\
重新缩进区域中的所有行(缩进区域)。
回答by jtahlborn
i wrote a function myself to do this for xml, which works well in nxml-mode. should work pretty well for html as well:
我自己编写了一个函数来为 xml 执行此操作,该函数在 nxml 模式下运行良好。对于 html 也应该可以很好地工作:
(defun jta-reformat-xml ()
"Reformats xml to make it readable (respects current selection)."
(interactive)
(save-excursion
(let ((beg (point-min))
(end (point-max)))
(if (and mark-active transient-mark-mode)
(progn
(setq beg (min (point) (mark)))
(setq end (max (point) (mark))))
(widen))
(setq end (copy-marker end t))
(goto-char beg)
(while (re-search-forward ">\s-*<" end t)
(replace-match ">\n<" t t))
(goto-char beg)
(indent-region beg end nil))))
回答by nevcx
You can do a replace regexp
你可以做一个替换正则表达式
M-x replace-regexp
\(</[^>]+>\)
C-q-j
Indent the whole buffer
缩进整个缓冲区
C-x h
M-x indent-region
回答by abhillman
This question is quite old, but I wasn't really happy with the various answers. A simple way to re-indent an HTML file, given that you are running a relatively newer version of emacs (I am running 24.4.1) is to:
这个问题已经很老了,但我对各种答案并不满意。假设您运行的是相对较新版本的 emacs(我运行的是 24.4.1),重新缩进 HTML 文件的一种简单方法是:
- open the file in emacs
- mark the entire file with
C-x h
(note: if you would like to see what is being marked, add(setq transient-mark-mode t)
to your.emacs
file) - execute
M-x indent-region
- 在 emacs 中打开文件
- 标记整个文件
C-x h
(注意:如果您想查看被标记的内容,请添加(setq transient-mark-mode t)
到您的.emacs
文件中) - 执行
M-x indent-region
What's nice about this method is that it does not require any plugins (Conway's suggestion), it does not require a replace regexp (nevcx's suggestion), nor does it require switching modes (jfm3's suggestion). Jay's suggestion is in the right direction —?in general, executing C-M-q
will indent according to a mode's rules — for example, C-M-q
works, in my experience, in js-mode
and in several other modes. But neither html-mode
nor nxml-mode
do not seem to implement C-M-q
.
这种方法的好处在于它不需要任何插件(Conway 的建议),它不需要替换正则表达式(nevcx 的建议),也不需要切换模式(jfm3 的建议)。Jay 的建议是正确的方向——一般来说,执行C-M-q
会根据模式的规则缩进——例如C-M-q
,根据我的经验,在js-mode
其他几种模式中有效。但似乎html-mode
也nxml-mode
没有实施C-M-q
。
回答by Aaron Hall
In emacs 25, which I'm currently building from source, assuming you are in HTML mode, use
Ctrl-x
h
在我目前正在从源代码构建的 emacs 25 中,假设您处于 HTML 模式,请使用
Ctrl-x
h
to select all, and then press Tab.
选择全部,然后按Tab。
回答by Chris Conway
回答by Geoff
You can pipe a region to xmllint (if you have it) using:
您可以使用以下方法将区域通过管道传输到 xmllint(如果有):
M-|
Shell command on region: xmllint --format -
The result will end up in a new buffer.
结果将在一个新的缓冲区结束。
I do this with XML, and it works, though I believe xmllint needs certain other options to work with HTML or other not-perfect XML. nxml-mode will tell you if you have a well-formed document.
我使用 XML 执行此操作,并且它有效,但我相信 xmllint 需要某些其他选项才能使用 HTML 或其他不完美的 XML。nxml-mode 会告诉你是否有一个格式良好的文档。
回答by user1454331
The easiest way to do it is via command line.
最简单的方法是通过命令行。
- Make sure you have tidy installed
- type
tidy -i -m <<file_name>>
- 确保你有 tidy 安装
- 类型
tidy -i -m <<file_name>>
Note that -m
option replaces the newly tidied file with the old one. If you don't want that, you can type tidy -i -o <<tidied_file_name>> <<untidied_file_name>>
请注意,该-m
选项会用旧文件替换新整理的文件。如果你不想这样,你可以输入tidy -i -o <<tidied_file_name>> <<untidied_file_name>>
The -i
is for indentation. Alternatively, you can create a .tidyrc
file that has settings such as
的-i
是缩进。或者,您可以创建一个.tidyrc
具有以下设置的文件
indent: auto
indent-spaces: 2
wrap: 72
markup: yes
output-xml: no
input-xml: no
show-warnings: yes
numeric-entities: yes
quote-marks: yes
quote-nbsp: yes
quote-ampersand: no
break-before-br: no
uppercase-tags: no
uppercase-attributes: no
This way all you have to do is type tidy -o <<tidied_file_name>> <<untidied_file_name>>
.
这样你所要做的就是输入tidy -o <<tidied_file_name>> <<untidied_file_name>>
.
For more just type man tidy
on the command line.
有关更多信息,只需man tidy
在命令行上输入即可。