Michał Bendowski
Michał Bendowski

Reputation: 2791

In IntelliJ's XML editor, how to break a tag into multiple lines?

When I have code like this (| represents a cursor):

<tag>|</tag>

and press enter, I get this:

<tag>
  |
</tag>

My question is, how can I quickly get from this:

<tag>text</tag>

to this:

<tag>
  text
</tag>

Upvotes: 3

Views: 927

Answers (1)

Javaru
Javaru

Reputation: 32006

There is not a built in way to do this. However, if your markup is:

<tag>
    text</tag>

After running a code reformat (Ctrl+Alt+L) you will have this:

<tag>
    text
</tag>

You can take can take advantage of this in a macro. Off the top of my head, I can not think of a macro that will handle a single word value (without any spaces between the element tags and the value) and a multi-word value. Below is the macro that will do this for a multi-word value. For a single word value, you would drop step 2. Place your cursor in the value/text area of the element. Start a macro recording (Edit > Macro >Start Recording) and record the following macro:

  1. Ctrl+W - Select word
  2. Ctrl+W - Select the entire element value
  3. (Left Arrow) - move the cursor to the start of the selection, and thus at the end of the opening tag before the element value.
  4. Enter - add a line break
  5. Ctrl+Alt+L - reformat code (opens dialog)
  6. Ctrl+Enter - equivalent to clicking OK

Stop the macro recording, and name it. Then assign a keyboard shortcut to it (Settings > IDE Settings > Keymap > Macros)

Note that the above will run the reformat on the entire file. If you only want to run it on the tag in question, add three Ctrl+W commands between step 4 & 5 to select just that element and its value. Then add an Alt+S before Step 6 (to ensure the "Selected Text" option in the dialog is always selectes).

Upvotes: 2

Related Questions