2

After having created many files already, I changed my Eclipse configuration to use spaces instead of tabs. It works: new things I write use spaces. But files I wrote yesterday still have tabs, of course. I want to convert them from tabs to spaces.

For Java files it is easy: Open file, press CTRL+a, press CTRL+i, done.

But how to do so for XML files? Unlike Java, with XML files CTRL+a/CTRL+i not only converts tabs but also tries to "improve formatting", which wrecks things:

Initial code:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
<!--
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    (the "License"); you may not use this file except in compliance with
    the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->
<beans>
    <bean id="webscript.helloworld.get"
          class="com.example.HelloWorldWebScript"
          parent="webscript">
    </bean>
</beans>

After CTRL+a/CTRL+i (or CTRL+SHIFT+f):

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor 
 license agreements. See the NOTICE file distributed with this work for additional 
 information regarding copyright ownership. The ASF licenses this file to 
 You under the Apache License, Version 2.0 (the "License"); you may not use 
 this file except in compliance with the License. You may obtain a copy of 
 the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required 
 by applicable law or agreed to in writing, software distributed under the 
 License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
 OF ANY KIND, either express or implied. See the License for the specific 
 language governing permissions and limitations under the License. -->
<beans>
 <bean id="webscript.helloworld.get" class="com.example.HelloWorldWebScript"
  parent="webscript">
 </bean>
</beans>

QUESTION: For an XML file in Eclipse, how to convert tabs to spaces, with no other changes?

Community
  • 1
  • 1
Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
  • Can't check for now and this is an old post so not sure this is still valid but [Format XML code in Eclipse](http://stackoverflow.com/q/4527474/4391450) or a more manual solution [eclipse xml editor indent using spaces](http://stackoverflow.com/questions/31747059/eclipse-xml-editor-indent-using-spaces) – AxelH Mar 10 '17 at 06:56
  • @AxelH: Not duplicate. http://stackoverflow.com/questions/31747059/eclipse-xml-editor-indent-using-spaces asks how to change settings, which I have done already. http://stackoverflow.com/q/4527474/4391450 only has Ctrl+Shift+F answers, and as you know Ctrl+Shift+F wrecks all formatting (just try with my example code above). Please re-read my question: I have already set Eclipse to use spaces instead of tabs, and I want a command that only fixes spacing without doing any other formatting. – Nicolas Raoul Mar 10 '17 at 07:50
  • My bad, misread the shortcut you used. I see two solution, with a script or try to create a new format in eclipse, that will only edit the tabs, disable everything else. I can't test because the office don't use eclipse unfortunatly so not sure if every format option can be disabled. Your problem in the example is the column length property, set an huge value to fixe it – AxelH Mar 10 '17 at 07:55

3 Answers3

3

You are in the right way, but first of all, you have to customize the XML format:

  • Open Window > Preferences > XML > XML files > Editor.
  • Check format comments and uncheck Join lines. (This should indent comments and remove adjacent blanks and so, preserving the existing newlines. But if you feel uncomfortable with the result, uncheck format comments.)
  • Select Indent using spaces and set the indentation size.

Now you can format your XML files by pressing [SHIFT][CTRL][F].

Eclipse XML Formatting preferences

Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
Little Santi
  • 8,563
  • 2
  • 18
  • 46
  • I added a screenshot, does the configuration show what you meant? Unfortunately, in addition to just fixing indentation, it also joins XML attribute lines, so for instance in my example, `id` and `class` get on the same line. While I could check `Split multiple attributes each on a new line`, in some of my files with many short attributes I don't want that. I was hoping Eclipse had an XML formatter that behaves like the Java formatter: Fix the indentation and do nothing else. Thanks anyway, that's probably the closest to my dream formatter! – Nicolas Raoul Mar 14 '17 at 09:12
  • Yes, that was what I meant. And again yes: I'm afraid that's the closest you can get. Neither Eclipse formatters are perfect! :-( – Little Santi Mar 14 '17 at 11:19
1

Well, if you are doing it manually, and don't want an automatic script, just write a tab in your XML file, copy it then ctrl + f > Replace, paste the tab into find what and space in replace with, then just replace all. That's if you are not trying to replace it with a script.

Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
T.Lowly
  • 54
  • 1
  • 7
  • That is an option indeed. I still hope there is an easier shortcut, but will accept this if nothing better is available. – Nicolas Raoul Mar 10 '17 at 07:52
  • well or, you could find a script for it, I mean there should be plenty of scrpts that would do that, for one I think its not really that hard to make, it would just demand to upload and download trough PHP side, atleast I would only know how to make it trough PHP only haha, or there should be scripts out there that uses .bat some ++ coders maybe wrote it allready. :) – T.Lowly Mar 10 '17 at 08:03
1

Search -> Search... (or Ctrl+H) -> File Search,

check Regular expression,

set \t as Containing text:,

set *.xml as File name patterns,

hit Replace..., set (put spaces as many as you need) -> OK.

Done. Enjoy.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143