Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Vim to recognize XML

I would like Vim to help me indent my XML files like my C code. However, when I use

gg=G

It just sets everything to the left. Do I need to designate a syntax? Is XML recognized as a language?

like image 401
IslandCow Avatar asked Sep 29 '11 17:09

IslandCow


2 Answers

Put

filetype plugin indent on

in your .vimrc to have Vim automatically identify .xml files as xml. You might need to put

set nocompatible

before that.

If the file extension is not .xml, you can make Vim threat it like xml by using

: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.

like image 103
Idan Arye Avatar answered Nov 07 '22 17:11

Idan Arye


Yep, :set syntax=xml should work. In vim 7.3, this sets :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).

Then, gg=G should work.

like image 6
nobody Avatar answered Nov 07 '22 16:11

nobody