简体   繁体   English

在Intellij IDEA中,如何用新行替换文本?

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

Say I wanted to replace all commas with commas and a new line using Intellij IDEA's replace function. 假设我想使用Intellij IDEA的替换功能用逗号和新行替换所有逗号。 What do I put in the search box? 我在搜索框中放了什么? In vim I'd use &\\r 在vim中我会使用&\\ r \\ n

You need to check the Regex box and use "\\n" for the new line character: 您需要检查正则表达式框并使用“\\ n”作为新行字符:

在此输入图像描述

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 编辑:自IntelliJ 15以来缺少多线按钮,但您可以通过单击文本字段并按Alt+EnterCtrl+Shift+Enter来启用它

Hit CTRL+F and check the regex checkbox. CTRL+F并选中regex复选框。 Then search for , and replace it with ,\\n . 然后搜索,并将其替换,\\n

The easiest way that I have done it is to use the regular expression form of replace. 我做的最简单的方法是使用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. 有可能你不想替换{ ,但如果你愿意的话,请继续我的逃避。

For those looking for the old multiline replace in inteliJ with version > 15.x. 对于那些寻找旧版多行替换版本> 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. 它似乎有点隐藏,但如果您选择多行>单击CTRL+F ,然后立即单击CTRL+R您将看到原始多行替换。

This is working on Mac IntelliJ 2016.1.3 with ⌘+F > ⌘+R 这适用于Mac IntelliJ 2016.1.3 ,⌘+ F>⌘+ R.

在此输入图像描述

对于Mac上的Intellij Ultimate 2017.3, command-shift-enter有效

On intellij Ultimate 2017.1: 在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. 我将光标放在我想要输入替换文本的字段中,然后单击Ctrl-Shift + Enter。 Here I just hit return 在这里,我只是回击

在此输入图像描述

A clean approach would be to add (?m) in front of the regular expression, which turns on the multi line mode. 一个干净的方法是在正则表达式前添加(?m) ,这将打开多行模式。 This has the advantage that you can also use it in the global file search (Ctrl-Shift-F). 这样做的好处是您还可以在全局文件搜索(Ctrl-Shift-F)中使用它。

Example: (?m)\\{(.|\\n)*?\\} searches for multi-line blocks surrounded by curly braces. 示例:( (?m)\\{(.|\\n)*?\\}搜索由大括号括起来的多行块。

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 所以我在Node.js中遇到了问题,我想将拒绝分成呼叫到日志并拒绝清除

reject(error)

into

appLogger.log(error, 'error') reject(error)

In normal mode, I did find and replace 在正常模式下,我确实找到并替换了

Find: reject(error) 查找: reject(error)

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

Then in regex mode I did a second find and replace: 然后在正则表达式模式下我做了第二次查找和替换:

Find: \\\\n 发现: \\\\n

Replace \\n 替换\\n

Ctrl + Shift + R while the replaced text is selected : 选择替换文本时 按Ctrl + Shift + R

This works for Replace in Path (WebStorm 2018.2.3): 这适用于路径替换 (WebStorm 2018.2.3):

在此输入图像描述

see here 看到这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM