Vim formatting using gg=G with xml

独自空忆成欢 提交于 2019-12-20 13:59:07

问题


I have a correctly formatted xml file, and following the command given as an answer here:

How can I autoformat/indent C code in vim?

I am trying to auto indent my file to display correct nesting.

Before I tried to use this command I set the file type to xml using :set ft=xml as the file I started with has an extension of .mm, and also :set nowrap.

Here is my ~/.vimrc file:

syntax on
set history=1000
set smartindent
set tabstop=2
set shiftwidth=2
set expandtab

How come when I issue gg=G, I get a message saying 54 lines indented, yet my file remains un-nested?


回答1:


in case you wanna try Vim's own XML indenter thing, you can...

:filetype indent on         (load indent files for specific file types etc.)
:e                          (to reload the current buffer)

this will load the vimscript at $VIMRUNTIME/indent/xml.vim

then when you do

:set indentexpr?        

...it'll say indentexpr=XmlIndentGet(v:lnum, 1)

~~

xmllint is better though, see... http://vim.wikia.com/wiki/VimTip349

i have handy keybinding like this for it in my .vimrc!

" one or more lines:
vmap ,px !xmllint --format -<CR>

" pretty-print current line
nmap ,px !!xmllint --format -<CR>



回答2:


Try typing: :set equalprg?. If it says equalprg= it means that you do not have a program set for xml indentation, so it's probably doing some dumb default action. Here's a guide for setting xmllint as your formatter: http://ku1ik.com/2011/09/08/formatting-xml-in-vim-with-indent-command.html



来源:https://stackoverflow.com/questions/11655383/vim-formatting-using-gg-g-with-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!