In Intellij IDEA how do I replace text with a new line?

JavaIntellij Idea

Java Problem Overview


Say I wanted to replace all commas with commas and a new line using Intellij IDEA's replace function. What do I put in the search box? In vim I'd use &\r

Java Solutions


Solution 1 - Java

You need to check the Regex box and use "\n" for the new line character:

enter image description here

Solution 2 - Java

Use Multiline button, no Regex is needed.

edit: the multiline button is missing since IntelliJ 15, but you can enable it by clicking into the textfield and pressing Alt+Enter or Ctrl+Shift+Enter

Solution 3 - Java

Hit CTRL+F and check the regex checkbox. Then search for , and replace it with ,\n.

Solution 4 - Java

The easiest way that I have done it is to use the regular expression form of replace.

enter image description here

Chances are that you don't want to replace the {, but just keep in my escaping them if you do want to do so.

Solution 5 - Java

For Intellij Ultimate 2017.3 on Mac, command-shift-enter works

Solution 6 - Java

On intellij Ultimate 2017.1:

I didn't need regex. But I could make the multiline replace appear.

  • I entered \n in the field I wanted to replace
  • I placed my cursor in the field where I wanted to enter the replacement text, and clicked Ctrl-Shift + Enter. Here I just hit return

enter image description here

Solution 7 - Java

For those looking for the old multiline replace in inteliJ with version > 15.x. It seems somewhat hidden, but if you select multiple lines > click CTRL+F, then immediately click CTRL+R you are presented with the original multiline replace.

This is working on Mac IntelliJ 2016.1.3 with ⌘+F > ⌘+R

enter image description here

Solution 8 - Java

A clean approach would be to add (?m) in front of the regular expression, which turns on the multi line mode. This has the advantage that you can also use it in the global file search (Ctrl-Shift-F).

Example: (?m)\{(.|\n)*?\} searches for multi-line blocks surrounded by curly braces.

Solution 9 - Java

The is related but not exactly what you asked. But I needed it and I can imagine others do to. So I had the problem in Node.js where I wanted to split a reject into call into a log and reject for clarity

reject(error)

into

 reject(error)

In normal mode, I did find and replace

>Find: reject(error)

>Replace: appLogger.log(error, 'error')\n reject(error)

Then in regex mode I did a second find and replace:

>Find: \\n

>Replace \n

Solution 10 - Java

Ctrl + Shift + R while the replaced text is selected:

This works for Replace in Path (WebStorm 2018.2.3):

enter image description here

see here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBjornView Question on Stackoverflow
Solution 1 - JavaAndrey ChaschevView Answer on Stackoverflow
Solution 2 - JavaMeoView Answer on Stackoverflow
Solution 3 - JavakmeraView Answer on Stackoverflow
Solution 4 - JavapickypgView Answer on Stackoverflow
Solution 5 - Javawiena wuView Answer on Stackoverflow
Solution 6 - JavaSomaiah KumberaView Answer on Stackoverflow
Solution 7 - Javawired00View Answer on Stackoverflow
Solution 8 - JavaspheenikView Answer on Stackoverflow
Solution 9 - JavaDr Code MonkeyView Answer on Stackoverflow
Solution 10 - JavaFelixView Answer on Stackoverflow