Format XML code in Eclipse
Asked Answered
B

6

72

When I write .xml files in Eclipse, I often have to manually indent code. For example

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/background"
android:layout_height="fill_parent"
android:layout_width="fill_parent" />

After formatting it looks like this.

<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout   
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:background="@color/background"
      android:layout_height="fill_parent"   
      android:layout_width="fill_parent" />

How to automate this? I can't find formatting settings in Eclipse for XML.

Burck answered 24/12, 2010 at 17:11 Comment(2)
CTRL + A to select the entire text and then CTRL + SHIFT + FBluecollar
I know of this feature. Where do I set it up? I can't find it in Window->Preferences->XML. I don't like the way it cuts attributes.Burck
L
122

Ctrl+Shift+F

Will do it for you, and you can configure it in

Window -> Preferences -> XML -> Xml Files -> Editor
Leucoplast answered 24/12, 2010 at 17:20 Comment(5)
I know of this feature. Where do I set it up? I can't find it in Window->Preferences->XML. I don't like the way it cuts attributes.Burck
if options in Window->Preferences->XML->Xml Files->Editor not enough then I don't know the answerLeucoplast
Took me awhile to figure out there are actually two "sub-tabs" in the Eclipse XML Editor, "Design" and "Source", and this only applies to the "Source" one. The "sub-tabs" are underneath the actual editor tab/view.Columba
This does not solve the issue for me, There is no "Save Action" menu as there is for Java. If I have a document that already has Tabs and spaces in. When I click save, the tabs are not removed and converted to spaces.Dodona
and then "[v] Split multiple attributes each on a new line" and then ReStart eclipse.Vivavivace
B
38

As stated above, Ctrl+Shift+F will auto-format. However, by default Eclipse will split attributes in a naive way. If you're like me and prefer to have all the attributes on their own lines, go to Window→Preferences→XML→XML Files→Editor and check Split multiple attributes each on a new line.

Butterwort answered 13/3, 2011 at 17:35 Comment(0)
F
15

You need to open the file using the XML Editor, swap to Source view and then do the Ctrl + Shift + F trick.

Fought answered 2/8, 2011 at 10:50 Comment(1)
It's critical to open the XML file using the XML Editor in order for CTRL+Shift+F key combination to work.Avner
R
14

CMD + SHIFT + F for Macintosh users

Reduce answered 15/4, 2012 at 19:35 Comment(2)
this doesn't work for me under OSX Mavericks, using the Eclipse Luna Modeling Tools configuration.Hereabouts
This is the correct default key sequence for autoformat. It's possible to change it, or to have a plugin change/interfere with it. Which can be annoying!Sancha
S
3

open windows->preferences->xml files->editor enter some high number for line width, say 999 and then format the xml file with ctrl+shift+F.

You ll see the lines are aligned properly.

Sybarite answered 27/7, 2015 at 4:52 Comment(1)
this answer is already posted. then why are posting it again the same answer...??Applicative
R
-2

You can set the default XML editor to “Java Editor”:

  • Right-click on the XML
  • Open With
  • Other...
  • Java Editor

This applies all java formatting and controls over XML file.

Redvers answered 29/1, 2018 at 19:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.