Java IntelliJ IDEA 多行编辑方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1262737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 07:33:14  来源:igfitidea点击:

IntelliJ IDEA way of editing multiple lines

javaandroid-studiointellij-ideaidetext-editor

提问by hdx

I've seen this done in TextMate and I was wondering if there's a way to do it in IDEA.

我已经在 TextMate 中看到了这一点,我想知道是否有办法在 IDEA 中做到这一点。

Say I have the following code:

假设我有以下代码:

 leaseLabel = "Lease";
 leaseLabelPlural = "Leases";
 portfolioLabel = "Portfolio";
 portfolioLabelPlural = "Portfolios";
 buildingLabel = "Building";

What is the best way to append '+ "foo"' to every line? Column mode won't work since the lines are not correctly aligned on the right side... unless there is an easy way to right justify the text :P

将 '+ "foo"' 附加到每一行的最佳方法是什么?列模式将不起作用,因为右侧的行没有正确对齐......除非有一种简单的方法可以右对齐文本:P

采纳答案by shobull

Since Idea IntelliJ IDEA 13.1 there is possibility to edit multiple lines.

从 Idea IntelliJ IDEA 13.1 开始,可以编辑多行。

Use:

用:

Alt+ Shift+ Mouse click

Alt+ Shift+ 鼠标点击

for selection. More about this new improvement in IntelliJ blogpost here. Very useful feature.

供选择。更多关于 IntelliJ 博文中这项新改进的信息,请点击此处。非常有用的功能。

回答by Chris Kessel

I just use the macros for this sort of thing. I start recording the macro, do it once, then play back the macro on each line I want to modify. You'd be amazed at how fancy you can get with the macro record/playback feature.

我只是将宏用于此类事情。我开始录制宏,执行一次,然后在要修改的每一行上播放宏。您会惊讶于宏录制/回放功能的奇妙之处。

回答by Tom van Zummeren

In this case you can also just select the piece of code in which you want to do this and perform a replace on it. Replace:

";

with:

" + "foo";

So in case you didn't know: If you have text selected while you perform a replace (Ctrl+R or Cmd+R) it will only apply to the selected piece of text.

在这种情况下,您也可以只选择要执行此操作的代码段并对其执行替换。将:

";替换

为:

" + "foo";

因此,如果您不知道:如果您在执行替换(Ctrl+R 或 Cmd+R)时选择了文本,它将仅适用于选定的文本片段。

回答by Srneczek

What I usually use (NetBeans, but I believe it is simple to use in any IDE) is find&replace.

我通常使用的(NetBeans,但我相信在任何 IDE 中使用都很简单)是find&replace.

You just find ;\nand replace it with + $foo;\nthen you don't apply to ALL lines but you place cursor on the first line and you just hit "replace" button (depends on your IDE I suppose) 7 times to change 7 lines in no time. Easy and simple and it should be done with the most basic and the most advanced IDE you can find.

您只需找到;\n并替换它,+ $foo;\n然后您就不会应用于所有行,但是您将光标放在第一行,然后您只需点击“替换”按钮(我想取决于您的 IDE)7 次即可立即更改 7 行。简单易行,应该使用您能找到的最基本和最先进的 IDE 来完成。

EDIT: In IntelliJ (don't know if it works in other IDEs too) you can use your regexp search&replace to selection only so you can actually use "replace all"

编辑:在 IntelliJ(不知道它是否也适用于其他 IDE)中,您可以仅使用正则表达式搜索和替换进行选择,这样您就可以实际使用“全部替换”

回答by Alexander Bogushov

I use Column Selection Mode(Cmd+Shift+8on Mac) which allows to create multiple cursors via Shift+Upor Shift+Downthen edit all the lines together.

我使用列选择模式(Mac 上的Cmd+ Shift+ 8),它允许通过Shift+UpShift+创建多个光标,Down然后一起编辑所有行。

Starting from IntelliJ IDEA 14 there is also Clone Caret Above / Below:

从 IntelliJ IDEA 14 开始,还有Clone Caret Above/Below

  • Windows: Ctrl, Ctrl+Up/Down
  • MacOS: Option,Option+ Up/Down
  • 视窗:Ctrl, Ctrl+ Up/Down
  • MacOS: Option, Option+ Up/Down

(hold the second press of the modifier key, then press the arrow key)

(按住修改键的第二次按下,然后按箭头键)

回答by Rafael Eyng

It took me a while to find out, but on a Mac you can double-press Option(press it once, release, press it again, keep it pressed) and use Up/Downkeys to create/remove carets as you wish.

我花了一段时间才发现,但在 Mac 上,您可以双击Option(按下一次,松开,再次按下,按住不放)并使用Up/Down键根据需要创建/删除插入符号。

You can also hold Shift+Optionand click to create/remove carets at specific points.

您还可以按住Shift+Option并单击以在特定点创建/删除插入符号。

回答by DAle

Another keyboard-only approach. It's possible (since 13.1 version) to use Alt+J/ Shift+Alt+J(Ctrl+Gfor OS X) shortcuts for creating multiple carets. Alt+Jselects the next occurrence of the currently selected text and adds another caret.

另一种仅使用键盘的方法。可以(从 13.1 版本开始)使用Alt+ J/ Shift+ Alt+ J(OS X 为Ctrl+ G)快捷方式来创建多个插入符号。Alt+J选择当前选定文本的下一次出现并添加另一个插入符号。

  1. Select the first semicolon
  1. 选择第一个分号

enter image description here

enter image description here

  1. Then press Alt+Jfour times
  1. 然后按Alt+J四次

enter image description here

enter image description here

  1. Edit what you want to
  1. 编辑你想要的

enter image description here

enter image description here

  1. Press Escto return to the first line.
  1. Esc返回第一行。

enter image description here

enter image description here

回答by DimaSan

You could also do a vertical code block selection by clicking mouse wheeland dragging:

您还可以通过单击鼠标滚轮拖动进行垂直代码块选择:

enter image description here

enter image description here

回答by Gibolt

Place caret at end

将插入符号放在末尾

Windows:CTRL+ CTRL(Hold) + ↑ / ↓

视窗:CTRL+ CTRL(按住)+↑ / ↓

Mac:option+ option(Hold) + ↑ / ↓.

Mac:option+ option(按住)+ ↑ / ↓

To place caret at the end of rows: move caret to top row, clone down to bottom, and click END.

要将插入符号放在行尾:将插入符号移动到顶行,向下复制到底部,然后单击END

Change Multi-caret Hotkey

更改多插入符号热键

To add a custom Keymap, CTRL+SHIFT+A, type keymapand click on the one with Settingsas subtext. Search for Clone Caret Aboveand Clone Caret Below.

要添加自定义键盘映射,CTRL+ SHIFT+ A,键入keymap并单击带有Settings作为潜台词的那个。搜索Clone Caret AboveClone Caret Below

I mapped mine to ALT+SHIFT+↑ / ↓on Windows and ?+↑ / ↓on Mac.

我将我的映射到Windows上的ALT+ SHIFT+↑ / ↓和Mac上的?+ ↑ / ↓

Bonus

奖金

Try holding combinations of CTRL, SHIFT, and arrows for improved selection power.

尝试的保持组合CTRLSHIFT和箭头用于改善选择电源。

回答by K-Marouane

For mac users it's : ALT + SHIFT + Click

对于 mac 用户,它是: ALT + SHIFT + Click