Java 使用 Intellij 使用键盘或鼠标选择括号 () 或方括号 [] 或大括号 {} 之间的块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24261325/
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-14 11:11:21  来源:igfitidea点击:

Using Intellij to select block between parentheses () or brackets [] or curly brackets {} either with keyboard or mouse

javaintellij-ideaeditor

提问by Craig

I've started working with IntelliJ and I really like it, but there are a few features which I miss compared with Eclipse. One of which is selecting blocks between {}, (), or [] or jumping between the opening/closing of a block. For example, in eclipse if you double click just after an opening parentheses it will select everything up to the matching closing parentheses as in:

我已经开始使用 IntelliJ 并且我真的很喜欢它,但是与 Eclipse 相比,我错过了一些功能。其中之一是在 {}、() 或 [] 之间选择块或在块的打开/关闭之间跳转。例如,在 Eclipse 中,如果您在左括号后双击,它将选择匹配右括号之前的所有内容,如下所示:

method(item1, method2(itemA), item3,
  item4, item5);

Where if you double clicked after the opening parentheses method(|, then it would select everything up to the closing parentheses, right after item5. I have discovered that IntelliJ will select method bodies when you double click, but not regions inside of parentheses and not for class bodies.

如果您method(|在左括号之后双击,那么它会选择右括号之前的所有内容,就在 item5 之后。我发现 IntelliJ 会在您双击时选择方法主体,但不会选择括号内的区域,也不会选择类主体。

Also, in eclipse, you can jump between the end and beginning of a block by pressing Ctrl+Shift+P just after the opening/closing of the block. In IntelliJ (using eclipse key mapping), Ctrl+Shift+P simply selects everything up to the method's closing curly brace '}'. I've discovered that Ctrl+Shift+} works the way that I expect but only for curly braces {} and it also selects everything between the block be it a method or class rather than just moving the cursor.

此外,在 eclipse 中,您可以在块的打开/关闭后按 Ctrl+Shift+P 在块的末尾和开头之间跳转。在 IntelliJ(使用 eclipse 键映射)中,Ctrl+Shift+P 只需选择方法的右花括号“}”之前的所有内容。我发现 Ctrl+Shift+} 以我期望的方式工作,但仅适用于花括号 {} 并且它还选择块之间的所有内容,无论是方法还是类,而不仅仅是移动光标。

I can tell that IntelliJ is fairly sophisticated and customizable, but I can't figure out how to duplicate this feature from Eclipse. Any assistance would be appreciated in getting this functionality to work.

我可以说 IntelliJ 相当复杂且可定制,但我不知道如何从 Eclipse 复制此功能。在使此功能正常工作方面,我们将不胜感激。

Thanks in advance! Craig

提前致谢!克雷格

采纳答案by g-t

One of which is selecting blocks between {}, (), or [] or jumping between the opening/closing of a block.

其中之一是在 {}、() 或 [] 之间选择块或在块的打开/关闭之间跳转。

Let's assume you have cursor on itemA.

假设您在 itemA 上有光标。

Ctrl+W -> selected:

Ctrl+W -> 选中:

itemA

Ctrl+W -> selected:

Ctrl+W -> 选中:

method2(itemA)

Ctrl+W -> selected:

Ctrl+W -> 选中:

item1, method2(itemA), item3,
                item4, item5

Ctrl+W -> selected:

Ctrl+W -> 选中:

method(item1, method2(itemA), item3,
                item4, item5)

And so on. After that it would select whole method, class etc. Honestly this is the shortcut which I use most frequently. Unfortunately I sometimes try to use that in other applications - each web browser closes tab by pressing Ctrl+W :)

等等。之后它会选择整个方法,类等。老实说,这是我最常使用的快捷方式。不幸的是,我有时会尝试在其他应用程序中使用它 - 每个 Web 浏览器通过按 Ctrl+W 关闭选项卡 :)

Another thing is:

另一件事是:

  • Ctrl + {
  • Ctrl + }
  • Ctrl + {
  • Ctrl + }

It jumps to opening / closing bracket

它跳转到开/关括号

回答by zeeshan

In Mac, go to start of a block and do:

在 Mac 中,转到块的开头并执行以下操作:

Command + Shift + Alt + }

or go to end of a block and do:

或转到块的末尾并执行以下操作:

Command + Shift + Alt + {

In Windows do it as:

在 Windows 中这样做:

Ctrl + Shift + }

and

Ctrl + Shift + {

回答by JeB

I've been using Ctrl+W, but this is very annoying as it first selects a word, then an enclosing statement, then another enclosing statement and so on. You have to press Ctrl+Wjust too much times.

我一直在使用Ctrl+W,但这很烦人,因为它首先选择一个词,然后是一个封闭语句,然后是另一个封闭语句,依此类推。你必须按Ctrl+W太多次。

The best way I've figured for any block selection is:

我为任何块选择想到的最好方法是:

  1. Press Ctrl+{- this will take you to the beginning of the block
  2. Press Ctrl+Shift+}- this will select the whole block from the beginning to the end.
  1. Ctrl+{- 这将带您到块的开头
  2. Ctrl+Shift+}- 这将从头到尾选择整个块。

This way you're able to select a block of any size with any amount of nested blocks with few actions.

通过这种方式,您可以通过很少的操作选择具有任意数量嵌套块的任意大小的块。

回答by Mansoor Siddiqui

If you're using the Mac OS X 10.5+keymap, selecting the enclosing scope is alt+up. Pressing it repeatedly expands the selection by the next enclosing scope.

如果您使用的是Mac OS X 10.5+键盘映射,则选择封闭范围是alt+up. 重复按下它会通过下一个封闭范围扩大选择范围。

If you're using the Mac OS Xkeymap, this action is mapped to command+W.

如果您使用Mac OS X键盘映射,则此操作将映射到command+W.

回答by Mijo

I am using a workaround which works pretty well. Just beside the code block you will see minimize/maximize button (which is used for code folding, the minus and the plus sign you see beside the line numbers). You will find it on minus, as the code is unfolded. Just press on the minus, the whole code block will be minimized, and in one line. Choose that line, copy it, paste it, delete it or whatever you want to do with it :)

我正在使用一种效果很好的解决方法。就在代码块旁边,您将看到最小化/最大化按钮(用于代码折叠,您在行号旁边看到的减号和加号)。随着代码的展开,您会在减号上找到它。只需按减号,整个代码块将被最小化,并在一行中。选择该行,复制它,粘贴它,删除它或任何你想用它做的:)