Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim indent xml file

Tags:

vim

xml

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.

I am trying to correctly indent an file (xml). The command I use is:

gg=G  

or ggVG= (made this one up myself probably does something different ;))

My .vimrc is:

syntax on  filetype plugin indent on  set nu  
like image 486
Haagenti Avatar asked Jan 28 '14 14:01

Haagenti


People also ask

Does XML have indentation?

No, the indentation does not matter in XML documents.

What is Xmllint?

The xmllint program parses one or more XML files, specified on the command line as XML-FILE (or the standard input if the filename provided is - ). It prints various types of output, depending upon the options selected. It is useful for detecting errors both in XML code and in the XML parser itself.


1 Answers

I like Berei's answer. However, I think the following is a little more flexible in that you don't have to alter your vimrc file. Plus it is easier to format select portions of the XML file (something I happen to do a lot).

First, highlight the XML you want to format.

Then, in normal mode, type ! xmllint --format -

Your command-line at the bottom will look like this:

:'<,'>!xmllint --format -

Then hit enter.

Technical Explanation

The selected text is sent to the xmllint command, then --format'ed, and the results of xmllint are 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.

like image 134
Jesse Hogan Avatar answered Sep 20 '22 15:09

Jesse Hogan