让 Vim 识别 XML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7600860/
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
Getting Vim to recognize XML
提问by IslandCow
I would like Vim to help me indent my XML files like my C code. However, when I use
我希望 Vim 帮助我缩进我的 XML 文件,就像我的 C 代码一样。但是,当我使用
gg=G
It just sets everything to the left. Do I need to designate a syntax? Is XML recognized as a language?
它只是将所有内容都设置在左侧。我需要指定一个语法吗?XML 被识别为一种语言吗?
回答by Idan Arye
Put
放
filetype plugin indent on
in your .vimrcto have Vim automatically identify .xml files as xml. You might need to put
在你.vimrc有Vim的自动识别.xml文件为XML。你可能需要把
set nocompatible
before that.
在那之前。
If the file extension is not .xml, you can make Vim threat it like xml by using
如果文件扩展名不是 .xml,你可以使用 xml 来使 Vim 威胁它
:set filetype=xml
After you do that, Vim's autoindention (and syntax highlighting, and omnicomplete (that in xml just closes tags, but that's still something)) will work properly for xml.
在你这样做之后,Vim 的自动缩进(和语法高亮显示,和 omnicomplete(在 xml 中只是关闭标签,但这仍然是一些东西))将适用于 xml。
回答by nobody
Yep, :set syntax=xmlshould work. In vim 7.3, this sets :set indentexpr=XmlIndentGet(v:lnum,1).
是的,:set syntax=xml应该工作。在 vim 7.3 中,这将设置:set indentexpr=XmlIndentGet(v:lnum,1).
If you've got a one-line file, you may need to :%s/\(<[^>]*>\)/\1\r/g, to insert newlines after every tag (or split it differently).
如果您有一个单行文件,您可能需要:%s/\(<[^>]*>\)/\1\r/g在每个标签后插入换行符(或以不同方式拆分)。
Then, gg=Gshould work.
那么,gg=G应该工作。
回答by challenger
add this line to your .vimrc file:
将此行添加到您的 .vimrc 文件中:
:map <Space>fx :%s/\ </\r</g<cr>:%s/\ android/\randroid/g<cr>:g/^$/d<cr>gg=G`
to format click space fx
格式化点击 space fx

