168

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

11 Answers 11

220

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

enter image description here

2
  • And how can you remove an empty line using same regex. :)
    – muasif80
    Commented Sep 20, 2017 at 18:22
  • @muasif80 in notepad++ editor I can search for \n\n and replace with \n or search for YXYZ\n and replace with XYZ or \nABC and replace with just ABC ... depends on document, but in genereal can search for \n and in replace part leave that out
    – tgkprog
    Commented Dec 10, 2023 at 6:20
119

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

7
  • 6
    I'm using 15, and it looks like the multiline feature is no more. Why would they remove such a useful feature? Commented Jan 14, 2016 at 16:29
  • 4
    This still exists, select multiple lines, then click CTRL+F, then click CTRL+R, it seems to be a hidden feature, but still works in that case as previous versions
    – wired00
    Commented May 22, 2016 at 7:09
  • 11
    It's even easier now -- a visible return carriage arrow appears within the find or replace boxes -- clicking it will insert a newline. Commented Nov 15, 2017 at 16:02
  • is there any way to make it keep indentation? If I replace a string apples that starts at col 16 with apples\nbananas, bananas will be at column 0
    – Tom M
    Commented Nov 8, 2021 at 16:55
  • @TomM there is no way afaik, unless you write a plugin.
    – Meo
    Commented Mar 13, 2022 at 18:56
20

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

1
  • 1
    This answer is legitimate and work for search and replace in IntelliJ IDEA, PHPStorm and WebStorm where the user wants to replace an instance of \n with actual new line. Please do not downvote useful answers.
    – Bartekus
    Commented Apr 2, 2018 at 23:01
9

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

8

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.

3
  • Ha-ha, looks like everyone knows this. Commented Jan 19, 2014 at 17:16
  • It does, and it seems like you're faster with Paint :). PS: If you have not tried it, then I strongly suggest the Darcula! It is so much easier on my eyes.
    – pickypg
    Commented Jan 19, 2014 at 17:19
  • 1
    It's PicPick I'm using. I tried Idea Darcula Theme, but I guess I'm too much used to the default colors. Commented Jan 19, 2014 at 19:13
7

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

1
  • Command-Shift + Enter on Mac
    – akauppi
    Commented Mar 11, 2022 at 10:49
4

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

3
  • 3
    you just press ALT+Enter, with the focus in the textfield
    – Meo
    Commented May 22, 2016 at 7:17
  • Even with this trick it is not possible to enter multiple lines in the replace field. Commented Jun 22, 2016 at 11:16
  • Its working for me fine within IntelliJ 2016.1.3 on MAC, ⌘ + F > ⌘ + R
    – wired00
    Commented Jun 22, 2016 at 22:33
0

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.

0

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

appLogger.log(error, 'error') 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

1
  • 1
    If your answer is helpful, but not actually answering this question, it's better to ask a new question and add your answer there. We always welcome useful new questions and answers, and it's encouraged to answer your own question if you can :) Commented Sep 30, 2018 at 20:20
0

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

0

Working solution for Intellij Community or Ultimate.

Click Regex button - in the end

Intellij Regex for extended replacec

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.