How to use Multi-caret in Android studio

When working on an android project, often you want to edit multiple lines at the same time. For example, suppose you wish to annotate the properties of your model class with the JSON serializer (e.g., Gson or Moshi) as seen below.

data class User
    @Json(name="id") val id: Long,
    @Json(name="name") val name: String,
    @Json(name="url") val url: String
)

As you’ve noticed here, @Json(name=””) is common for all properties and can be made to write only once.

Meet Multi-caret

When typing, copying, or pasting in IntelliJ IDEA based editor, you can toggle multiple cursors so that your actions apply in several places simultaneously. Advanced editor actions, such as code completion and live templates are supported as well and will apply to each caret. 

To use multi-caret feature

1. Start editing by placing your caret (cursor) on the first line.

2. Click on additional lines you wish to set the caret at while pressing and holding Alt + Shift.

3. Type the necessary code.

4. Press Escape to remove multi-caret.

Your code will be written simultaneously on all lines.

Share with your friends

Leave a Reply

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