Friends of OpenJDK Today

Working with Multiple Carets in IntelliJ IDEA

November 14, 2021

Author(s)

  • Helen Scott

    Helen is a Java Developer Advocate at JetBrains. She has over 20 years’ experience in the software industry which has been gained in a variety of roles including developer, technical ... Learn more

Sometimes you need to modify multiple lines of code on separate lines inside IntelliJ IDEA with the same change. The fastest way to achieve that is with multiple carets. In IntelliJ IDEA you can:

  • Create multiple carets in a vertical line
  • Create multiple carets at the end of lines
  • Create multiple carets at any point in your code

Promoted Content

Step up your coding with the Continuous Feedback Udemy Course: Additional coupons are available

What do you know about the code changes that were just introduced into the codebase? When will you notice if something goes wrong?

Get Started Here!

Create Multiple Carets in a Vertical Line

Creating carets that are stacked vertically where you have content on different lines, like a list, can save you a lot of time when you're editing your code.

  • macOS press Option twice and hold it down the second time. Then use the arrow keys (up or down)
  • Windows press Ctrl twice and hold it down the second time. Then use the arrow keys (up or down)

Let's say you have defined some variables at the start of your class, but retrospectively you realise that their scope should be private, and since we're defining them here, they should also be final:

public String oak = "Oak";
public String sycamore = "Sycamore";
public String pine = "Pine";

You could click each one and replace public with private but there's a faster way in IntelliJ IDEA. You can press Option (macOS), or Ctrl (Windows/Linux) twice, holding it down the second time you press it and then use arrow keys to create a tower of vertical carets. You can then replace public with private just once, not three times. You can also add final since you're here:

Modifying variables with multiple carets

How about dealing with a big list that you've pasted into your code? How can you inline everything? First let's make an array for our data, then we can use the same clone caret operation before wrapping it in braces and completing the statement:

Inlining a long list

Finally, how about HTML tags? Again, it's the same process:

HTML tags and multiple carots

Create Multiple Carets at the End of Lines

Content at the start of your line is likely to be left-aligned, but that's not the case at the end of your line. We can deal with that by using the mouse to drag the caret down, instead of the vertical arrow keys.

Place your cursor to the far right of the furthest line of code and:

  • macOS hold down Option and drag the mouse down (or up)
  • Windows hold down Alt and drag the mouse down (or up)
    • Note that for Windows the shortcut for this one is Alt, not Ctrl

Let's say that you want to add something to the end of each of these strings, for example:

public final String lion = "Lives in Africa";
public final String stoat = "Lives in the United Kingdom";
public final String wolf = "Lives in Europe";

This time, instead of cloning the caret in a vertical line with the down arrow, hold down Option (macOS), or Alt (Windows/Linux) and use the mouse to place your caret to the right of the all the lines and then drag the mouse down. The caret will be placed at the end of each line. You can then add the text even though the lines are not in a straight line:

Multiple carets at the end of the line

Create Multiple Carets at Any Point in Your Code

And finally, what if you want multiple carets, but not in a stacked vertical line or at the end of each line? Again, the shortcut is similar:

  • macOS hold down Option and then Shift and click where you want the caret
  • Windows hold down Alt and then Shift and click where you want the caret

Let's say you want to add "Name" to these variables, but only to some of them:

Multiple carets at the end of the line

Summary

There's lots of places that multiple carets are helpful in IntelliJ IDEA, especially when you're working with lists. Just remember that for carets in a vertical line, press Option (macOS) and Ctrl (Windows/Linux) twice, holding it down the second time.

For carets to the right of a line hold down Option (macOS), or Alt (Windows/Linux) and drag the mouse pad/wheel down (or up).

For random carets, you add press and hold Option+Shift (macOS), or Alt+Shift (Windows/Linux), then click anywhere to place extra carets.

Multiple carets can also be created using Column Selection Mode. There is an example of this, and the techniques we talked about above, in this video from JetBrains, specifically at this time point. There are lots of other helpful tips in the video as well.

Topics:

Related Articles

View All

Author(s)

  • Helen Scott

    Helen is a Java Developer Advocate at JetBrains. She has over 20 years’ experience in the software industry which has been gained in a variety of roles including developer, technical ... Learn more

Comments (6)

Your email address will not be published. Required fields are marked *

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Save my name, email, and website in this browser for the next time I comment.

Syam Pillai

Great article. I never knew about this!! I only knew about the Column Selection Mode.
Thank you.

"Créer plusieurs cigurie à la fin des lignes de Intellij" Code Réponse - Coder les réponses

[…] La Source Étiquetteswhatever […]

"Créer plusieurs cigurie à tout moment de votre code" Réponse de code - Coder les réponses

[…] La Source Étiquetteswhatever […]

"Créer plusieurs cadres dans une ligne verticale" Réponse de code - Coder les réponses

[…] La Source Étiquetteswhatever […]

It’s My Last Day at JetBrains - Helen Scott

[…] Faster in IntelliJ IDEA for ICPC. Yes, I’ll move on soon, but I also created this blog on Working with Multiple Carets in IntelliJ IDEA which is still the most popular blog on foojay […]

Getting started with IntelliJ IDEA - Helen Scott

[…] Maybe you want to make the same change in multiple places; IntelliJ IDEA allows you to create multiple carets either in a stacked list or randomly as required. (⌥ | Ctrl) twice, holding it down the second time and then pressing the Down arrow to create a stack of carets or by clicking in your code to add a caret. You can learn more about this feature from this blog post on foojay. […]

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard