Vim 缩进 xml 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21408222/
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
Vim indent xml file
提问by Haagenti
I am learning Vim but I thought this was a simple task but I cannot get it to work. I have browser SO but the solutions are not working for me.
我正在学习 Vim,但我认为这是一项简单的任务,但我无法让它工作。我有浏览器,但解决方案对我不起作用。
I am trying to correctly indent an file (xml). The command I use is:
我正在尝试正确缩进文件 (xml)。我使用的命令是:
gg=G
or ggVG= (made this one up myself probably does something different ;))
或 ggVG=(我自己编造的可能做了一些不同的事情;))
My .vimrc is:
我的 .vimrc 是:
syntax on
filetype plugin indent on
set nu
回答by Jesse Hogan
I like Berei's answer. However, I think the following is a little more flexible in that you don't have to alter your vimrcfile. Plus it is easier to format select portions of the XML file (something I happen to do a lot).
我喜欢贝雷的回答。但是,我认为以下内容更加灵活,因为您不必更改vimrc文件。此外,格式化 XML 文件的选定部分更容易(我碰巧经常这样做)。
First, highlight the XML you want to format.
首先,突出显示要格式化的 XML。
Then, in normal mode, type ! xmllint --format -
然后,在正常模式下,键入 ! xmllint --format -
Your command-line at the bottom will look like this:
底部的命令行如下所示:
:'<,'>!xmllint --format -
:'<,'>!xmllint --format -
Then hit enter.
然后按回车。
Technical Explanation
技术说明
The selected text is sent to the xmllintcommand, then --format'ed, and the results of xmllintare placed over your selected text in vim. The -at the end of the command is for receiving standard input - which, in this case, is the selected text that vim sends to xmllint.
选定的文本被发送到xmllint命令,然后被--format'ed,并且结果xmllint被放置在 vim 中你选定的文本上。的-在命令的末尾是用于接收标准输入-其中,在这种情况下,是VIM发送到所选择的文本xmllint。
回答by Birei
Use an external program to indent your xmlfiles. In this case I've choosen xmllint, so set the command to the equalprgoption:
使用外部程序缩进您的xml文件。在这种情况下,我选择了xmllint,因此将命令设置为equalprg选项:
:set equalprg=xmllint\ --format\ -
Now you can execute
现在你可以执行
gg=G
to let xmllintformat your xmlfiles.
让xmllint格式化您的xml文件。
To get it every time you use vim, use an autocommandto set it.
要在每次使用vim时获取它,请使用 anautocommand来设置它。
autocommandfrom a comment below
autocommand从下面的评论
au FileType xml setlocal equalprg=xmllint\ --format\ --recover\ -\ 2>/dev/null
回答by KFL
A simple solution that I like which doesn't require any 3rd party tool is to insert a newline before each opening tag '<...>'. Then you can use standard vim auto-indentation. In short:
我喜欢的一个不需要任何 3rd 方工具的简单解决方案是在每个开始标记 '<...>' 之前插入一个换行符。然后你可以使用标准的 vim 自动缩进。简而言之:
%s/</\r</ggg=Gto auto indent
%s/</\r</ggg=G自动缩进
回答by ctrueden
Short answer:Turn on matchit.vim. You can add packadd matchitto your .vimrc, or use vim-sensible, which enables it by default.
简短回答:打开matchit.vim。您可以添加packadd matchit到您的.vimrc,或使用vim-sensible,默认情况下启用它。
Long answer:I have tried many strategies to auto-indent XML in Vim, including the xmllint approach discussed on the Vim wiki. The problem with xmllint(and the same approach with other external CLI tools such as xmlformatand tidy, too) is that they all insist on squeezing out blank newlines. If all you want is to indent your XML, without removing blank lines, comments and/or other intentional formatting, then xmllintis not the best choice.
长答案:我尝试了很多策略来在 Vim 中自动缩进 XML,包括Vim wiki 上讨论的xmllint 方法。问题xmllint(以及与其他外部 CLI 工具(如xmlformat和tidy)相同的方法)是它们都坚持挤出空白换行符。如果您只想缩进您的 XML,而不删除空行、注释和/或其他有意的格式,那么xmllint这不是最佳选择。
It turns out that Vim's equals (=) command already supports XML, as long as the matchit.vimscript is enabled. One super easy way to enable it is to adopt tpope's vim-sensible. Another is to add packadd matchitto your .vimrc. Then, XML formatting will "just work" out of the box.
事实证明,只要启用matchit.vim脚本,Vim 的 equals (=) 命令就已经支持 XML 。启用它的一种超级简单的方法是采用 tpope 的vim-sensible 。另一个是添加packadd matchit到您的.vimrc. 然后,XML 格式将“开箱即用”。
回答by jarederaj
This is easy to achieve once ~/.vimrc is setup properly.
一旦 ~/.vimrc 正确设置,这很容易实现。
Set the following options in ~/.vimrc:
在 ~/.vimrc 中设置以下选项:
filetype indent on
set smartindent
This allows you to move the cursor to the top of the file and indent to the end: gg=G
这允许您将光标移动到文件顶部并缩进到末尾:gg=G
You can also select the desired text with visual mode and hit = to indent it.
您还可以使用视觉模式选择所需的文本并点击 = 将其缩进。
回答by Cloud
In VIM, I auto-indent the whole file (using hard tabs) using the following command:
在 VIM 中,我使用以下命令自动缩进整个文件(使用硬制表符):
:%!XMLLINT_INDENT="^I" xmllint --format -
The ^Iis a single character you generate via: CTRL+v,CTRL+i
这^I是您通过以下方式生成的单个字符:CTRL+ v, CTRL+i
回答by Boris Brodski
I had the same problem. It turned out, that the line
我有同样的问题。原来,那条线
set viewdir=~\.vim\views\
in my .vimrccaused the problem. Just make sure, you don't have it.
在我.vimrc造成的问题。只要确定,你没有它。
回答by Cheetah Coder
Jesse Hogan's answer is good, but I'd rather more simple answer:
Jesse Hogan 的回答很好,但我更喜欢简单的回答:
While you are in vim and your XML file is opened, in normal mode write this:
当你在 vim 中并且你的 XML 文件被打开时,在正常模式下写:
:%!xmllint --format %
then press Enter. All of your XML file will be formatted.
然后按 Enter。您的所有 XML 文件都将被格式化。
remember you should have installed xmllintbefore.
请记住,您之前应该已经安装了xmllint。
回答by lehanh
for those one which is using coc.nvimplugin, you can install coc-xmlby :CocInstall coc-xml, then mapping format key in your config file: nmap <silent> fm <Plug>(coc-format). From now on, you can format not only xml file but other file very easy
对于那些使用coc.nvim插件的人,您可以coc-xml通过安装:CocInstall coc-xml,然后在配置文件中映射格式键:nmap <silent> fm <Plug>(coc-format). 从现在开始,您不仅可以格式化 xml 文件,还可以非常轻松地格式化其他文件

