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
You need to check the Regex box and use "\n" for the new line character:
-
And how can you remove an empty line using same regex. :)– muasif80Commented 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– tgkprogCommented Dec 10, 2023 at 6:20
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
-
6I'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
-
4This still exists, select multiple lines, then click
CTRL+F
, then clickCTRL+R
, it seems to be a hidden feature, but still works in that case as previous versions– wired00Commented May 22, 2016 at 7:09 -
11It'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 withapples\nbananas
, bananas will be at column 0– Tom MCommented Nov 8, 2021 at 16:55 -
For Intellij Ultimate 2017.3 on Mac, command-shift-enter
works
-
1This 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.– BartekusCommented Apr 2, 2018 at 23:01
Hit CTRL+F
and check the regex
checkbox. Then search for ,
and replace it with ,\n
.
The easiest way that I have done it is to use the regular expression form of replace.
Chances are that you don't want to replace the {
, but just keep in my escaping them if you do want to do so.
-
-
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.– pickypgCommented Jan 19, 2014 at 17:19
-
1It'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
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
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
-
3
-
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– wired00Commented Jun 22, 2016 at 22:33
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.
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
-
1If 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
Ctrl + Shift + R while the replaced text is selected:
This works for Replace in Path (WebStorm 2018.2.3):
see here