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.

2
  • 14
    CTRL + A to select the entire text and then CTRL + SHIFT + F Commented Dec 24, 2010 at 17:24
  • 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. Commented Dec 24, 2010 at 17:28

6 Answers 6

122

Ctrl+Shift+F

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

Window -> Preferences -> XML -> Xml Files -> Editor
Sign up to request clarification or add additional context in comments.

5 Comments

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.
if options in Window->Preferences->XML->Xml Files->Editor not enough then I don't know the answer
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.
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.
and then "[v] Split multiple attributes each on a new line" and then ReStart eclipse.
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.

Comments

16

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

1 Comment

It's critical to open the XML file using the XML Editor in order for CTRL+Shift+F key combination to work.
14

CMD + SHIFT + F for Macintosh users

2 Comments

this doesn't work for me under OSX Mavericks, using the Eclipse Luna Modeling Tools configuration.
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!
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.

1 Comment

this answer is already posted. then why are posting it again the same answer...??
-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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.