我使用emacs编辑我的xml文件(nxml-mode),并且机器生成的文件没有任何漂亮的标签格式.
我已经搜索了使用缩进打印整个文件并保存它,但无法找到自动方式.
有办法吗?或者至少有一些Linux编辑可以做到这一点.
小智 103
你甚至不需要编写自己的函数 - sgml-mode(一个gnu emacs核心模块)有一个内置的漂亮的打印函数(sgml-pretty-print ...),它接受区域的开始和结束参数.
如果你正在切割和粘贴xml,你发现你的终端正在任意位置切断线,你可以使用这个漂亮的打印机,先修复断线.
Chr*_*erg 85
如果您只需要非常缩进而不引入任何新的换行符,则可以indent-region
使用以下键击将命令应用于整个缓冲区:
C-x h
C-M-\
Run Code Online (Sandbox Code Playgroud)
如果你还需要引入换行符,那么打开和关闭标签在不同的行上,你可以使用由Benjamin Ferrari编写的以下非常好的elisp函数.我在他的博客上找到了它,并希望我可以在这里重现它:
(defun bf-pretty-print-xml-region (begin end)
"Pretty format XML markup in region. You need to have nxml-mode
http://www.emacswiki.org/cgi-bin/wiki/NxmlMode installed to do
this. The function inserts linebreaks to separate tags that have
nothing but whitespace between them. It then indents the markup
by using nxml's indentation rules."
(interactive "r")
(save-excursion
(nxml-mode)
(goto-char begin)
(while (search-forward-regexp "\>[ \\t]*\<" nil t)
(backward-char) (insert "\n"))
(indent-region begin end))
(message "Ah, much better!"))
Run Code Online (Sandbox Code Playgroud)
这不依赖于像Tidy这样的外部工具.
小智 35
Emacs可以使用M- |运行任意命令.如果您安装了xmllint:
"M- | xmllint --format - "将格式化所选区域
"Cu M- | xmllint --format - "也会这样做,用输出替换该区域
bub*_*bak 19
感谢上面的Tim Helmstedt,我做了这样的事情:
(defun nxml-pretty-format ()
(interactive)
(save-excursion
(shell-command-on-region (point-min) (point-max) "xmllint --format -" (buffer-name) t)
(nxml-mode)
(indent-region begin end)))
Run Code Online (Sandbox Code Playgroud)
快速而简单.非常感谢.
Tal*_*Kit 19
用于引入换行符然后漂亮的打印
M-x sgml-mode
M-x sgml-pretty-print
Run Code Online (Sandbox Code Playgroud)
这是我对Benjamin Ferrari的版本做的一些调整:
search-forward-regexp
没有指定一个结束,所以它会在东西运营从区域的开始到缓冲区的末尾(而不是区域的结束)end
正如Cheeso指出的那样,现在正确递增.<tag></tag>
它们之间插入一个中断,从而修改它的值.是的,从技术上讲,我们在这里修改一切的价值,但空的开始/结束更有可能是重要的.现在使用两个单独的,稍微严格的搜索来避免这种情况.仍然有"不依靠外部整洁",等等.但是,它需要cl
的incf
宏.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; pretty print xml region
(defun pretty-print-xml-region (begin end)
"Pretty format XML markup in region. You need to have nxml-mode
http://www.emacswiki.org/cgi-bin/wiki/NxmlMode installed to do
this. The function inserts linebreaks to separate tags that have
nothing but whitespace between them. It then indents the markup
by using nxml's indentation rules."
(interactive "r")
(save-excursion
(nxml-mode)
(goto-char begin)
;; split <foo><foo> or </foo><foo>, but not <foo></foo>
(while (search-forward-regexp ">[ \t]*<[^/]" end t)
(backward-char 2) (insert "\n") (incf end))
;; split <foo/></foo> and </foo></foo>
(goto-char begin)
(while (search-forward-regexp "<.*?/.*?>[ \t]*<" end t)
(backward-char) (insert "\n") (incf end))
(indent-region begin end nil)
(normal-mode))
(message "All indented!"))
Run Code Online (Sandbox Code Playgroud)
一种方法是如果您有以下格式的东西
<abc> <abc><abc> <abc></abc> </abc></abc> </abc>
Run Code Online (Sandbox Code Playgroud)
在Emacs中,试试吧
M-x nxml-mode
M-x replace-regexp RET > *< RET >C-q C-j< RET
C-M-\ to indent
Run Code Online (Sandbox Code Playgroud)
这将在xml示例上面缩进到下面
<abc>
<abc>
<abc>
<abc>
</abc>
</abc>
</abc>
</abc>
Run Code Online (Sandbox Code Playgroud)
在VIM中你可以做到这一点
:set ft=xml
:%s/>\s*</>\r</g
ggVG=
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
归档时间: |
|
查看次数: |
38875 次 |
最近记录: |