Vim indent xml file

I am learning Vim, but I thought it was a simple task, but I cannot get it to work. I have a SO browser, but solutions for me do not work.

I am trying to properly defer a file (xml). The command I use:

gg=G 

or ggVG = (did it himself, probably doing something else;))

My .vimrc:

 syntax on filetype plugin indent on set nu 
+58
xml vim
Jan 28 '14 at 14:25
source share
7 answers

I like Breya's answer. However, I think the following is a little more flexible since you do not need to modify your vimrc file. It is also easier to format individual parts of an XML file (something that I often do).

First select the XML you want to format.

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

Your line at the bottom will look like this:

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

Then press enter.

Technical explanation

The selected text is sent to the xmllint , then --format 'ed, and the xmllint results are placed above the selected text in vim. - at the end of the command - to accept standard input - which in this case is the selected text that vim sends to xmllint .

+111
Jan 29 '14 at 18:27
source share

Use an external program to indent xml files. In this case, I selected xmllint , so set the command to the equalprg parameter:

 :set equalprg=xmllint\ --format\ - 

Now you can execute

 gg=G 

to xmllint format your xml files.

To get it every time you use vim , use autocommand to install it.

autocommand from the comment below

 au FileType xml setlocal equalprg=xmllint\ --format\ --recover\ -\ 2>/dev/null 
+46
Jan 28 '14 at 14:41
source share

A simple solution that I like, which does not require a third-party tool, is to insert a new line before each opening tag '<...>'. Then you can use standard vim auto-indexing. In short:

  • %s/</\r</g
  • gg=G indent
+15
Sep 04 '15 at 20:07 on
source share

Short answer: Use vim-sensible , which includes matchit.vim by default.

Long answer: I tried many strategies for automatically indenting 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 like xmlformat and tidy ) is that they all insist on extruding blank lines. If you want only XML indentation without removing blank lines, comments, and / or other intentional formatting, then xmllint not the best choice.

It turns out that the Vim equals (=) command already supports XML if matchit.vim script is enabled. One very easy way to enable it is to accept tpope vim-sensible . Then XML formatting will β€œjust work” out of the box.

+8
Feb 06 '15 at 12:34
source share

This is easy to achieve if ~ / .vimrc is configured correctly.

Set the following options in ~ / .vimrc:

 filetype indent on set smartindent 

This allows you to move the cursor to the top of the file and back to the end: gg = G

You can also select the desired text with a visual mode and press = to indent.

+6
Jun 05 '15 at 20:30
source share

In VIM, I automatically print the entire file (using hard tabs) using the following command:

 :%!XMLLINT_INDENT="^I" xmllint --format - 

^I is the one character you create with: CTRL + v , CTRL + i

+4
May 5 '16 at 15:36
source share

I had the same problem. It turned out that the line

 set viewdir=~\.vim\views\ 

in my .vimrc caused this problem. Just make sure you don't have one.

+1
Oct 14 '15 at 15:16
source share



All Articles