165

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.

janw
  • 8,758
  • 11
  • 40
  • 62
Bjorn
  • 69,215
  • 39
  • 136
  • 164

10 Answers10

215

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

enter image description here

Andrey Chaschev
  • 16,160
  • 5
  • 51
  • 68
115

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

Meo
  • 12,020
  • 7
  • 45
  • 52
  • 6
    I'm using 15, and it looks like the multiline feature is no more. Why would they remove such a useful feature? – Matthew Cornell Jan 14 '16 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 May 22 '16 at 07: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. – Louis St-Amour Nov 15 '17 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 Nov 08 '21 at 16:55
  • @TomM there is no way afaik, unless you write a plugin. – Meo Mar 13 '22 at 18:56
  • works flawlessly! – Gaurav Jun 08 '22 at 15:35
17

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

wiena wu
  • 181
  • 1
  • 2
  • 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 Apr 02 '18 at 23:01
9

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

kmera
  • 1,725
  • 10
  • 22
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.

pickypg
  • 22,034
  • 5
  • 72
  • 84
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

Somaiah Kumbera
  • 7,063
  • 4
  • 43
  • 44
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

wired00
  • 13,930
  • 7
  • 70
  • 73
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.

spheenik
  • 2,129
  • 1
  • 13
  • 12
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
    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 :) – Max Vollmer Sep 30 '18 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

Felix
  • 3,999
  • 3
  • 42
  • 66