简体   繁体   中英

IntelliJ IDEA - multiple carets?

Sometimes I have to write Java class where I need to define multiple fields of the same type. For example, I know that I will need to duplicate private final String on the next 4 lines.

Is it possible to spawn multiple carets in IntelliJ editor, so I can type on mutliple lines at the sime time?

IntelliJ IDEA 14.1 & 15

You can press Alt + Shift and using the mouse left click you can put many carets.

Eg:

长插入符号 1

will become

长插入符号 2

typing "added" only once.

If you want to have a continuous vertical line , it's enough to press Alt + drag your mouse vertically . If there are lines with a length lower than the current position, the vertical line will be broken (carets will be placed at the end of every line) if "Allow placement of caret after end of line" is disabled (in File > Settings... > Editor > General > Virtual Space).

Another way to add a continuous vertical line is to hit Ctrl twice and then press up or down arrow key (supported in JetBrains 2016.X products, and possibly earlier).

If you cannot get the shortcuts working, check what they're currently assigned to under Settings > Keymap Add or Remove Caret , Clone Caret Above and Clone Caret Below . If they look right and you're on Linux, your window manager may be capturing the combination, eg, for Alt + window drag operations.

See :

I think "Column Selection Mode" could help you. You can enable it in the context menu in the editor. Then you can select multiple lines and type same text at once.

Column Select

For Windows, you can use CTRL + CTRL (Hold)+ ↑ / ↓ . For Mac, replace the CTRL with .

Change Multi-caret Hotkey

To add a custom Keymap, CTRL + SHIFT + A , type keymap and click on the one with Settings as subtext. Search for Clone Caret Above and Clone Caret Below .

I mapped mine to ALT + SHIFT + ↑ / ↓ .

Bonus

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

On Windows:

  • You can enable the Column Selection Mode ( Alt + Shift + Insert )
  • And then Shift + / can select multiple columns
  • Hit Esc to go back to single cursor

Add/remove a caret: Alt + Shift + Mouse Click

Remove all carets: Esc

As for now(2018.9), you just:

  • Press middle mouse key to create a four-rows-high caret
  • Type what you want

Or,

  • Create the first line,
  • Press the "Duplicate entire line" key combination to create a new, identical line.

By the way, I think "multiple caret" should be like carets enabling me to edit different locations which are not necessarily vertically aligned. In Sublime Text you can press Ctrl and click. And in IntelliJ you can only "Clone caret up/down", that is to say, they are vertically aligned, which is not flexible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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