In Intellij IDEA how do I replace text with a new

2019-02-01 15:18发布

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

10条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-02-01 15:47

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楼-- · 2019-02-01 15:50

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

查看更多
别忘想泡老子
4楼-- · 2019-02-01 15:52

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

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-02-01 15:52

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.

查看更多
女痞
6楼-- · 2019-02-01 15:56

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

查看更多
家丑人穷心不美
7楼-- · 2019-02-01 16:01

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

enter image description here

查看更多
登录 后发表回答