As Programmers, We encounter numerous situations where we have a specific type of code that needs copy-pasting, Where each part shares majority of it’s code with the others and differs in small bits.

Android Studio (and of course, other JetBrains products like IntelliJ-IDEA) have a feature called Multiple Selections (Borrowed from the beloved Sublime Text Editor) which comes handy at these situations.This article is just a reminder to use that feature whenever possible. Please note that this article is written with Android-Styling in mind, but it can be used in any other language with similar situation.

I recommend installing String Manipulation Plugin as it comes very handy working with multi-type-text code (camelHumps for code, snake case for xml).

Sample page that in android world, has 5 EditTexts

Let’s say we want to create a page that has multiple text fields sharing the same characteristics, with small difference.

For the sake of this article, let’s assume we are using multiple EditTexts in a Vertical LinearLayout . Each field is filled by it’s attribute in the UserModel.

We need to create 5 fields: Name, Last Name, Email, Website and Business. Let’s begin by creating a scratch file (Either by Ctrl-Alt-Shift-Insert or right click in the files area and then create scratch file). You don’t need any specific scratch file type, You can choose whatever (anything works) and type each of those fields’ logical name into the scratch file one line at a time. lets also create the base View for them.

Create a scratch file and put the basic structure in it

It’s time to use our nifty little feature! Select and Copy the part of our view leading to id=”@+id/ . Now let’s go to your layout, create 5 lines (5 Enters) and Multi Select those five lines (Using Middle mouse button drag or multiple Alt-Click ) and paste our copied code (The result might look a bit ugly right now).

What we want to create is 5 EditTexts , each with it’s own id and hint. To add the id part, Multi-select the beginnings of the names, and add the part leading to the id.

Now go back to the scratch file and Multi select the names and copy them here. If you want lower cased names, I suggest using the String Manipulation Plugin. After copying you can Shift-Select you names in the layout and go to Edit > String Manipulation > TO snake_case (Small suggestion, add a shortcut for snake_case like Ctrl-Shift-Q , It will come handy),. You can copy paste other parts of the layout and do the same. You can Multi-line-Edit your copied code to add/remove stuff (like string/ for the hint).

Don’t forget to Reformat your code after completing this layout.

What Like about this feature is that apart from the regular copy-paste and edit, code completion also works and you could do the EditText part with typing only. This is more useful in code where you could, For example, getText of those views like below.

As a last quick-note, Don’t forget the Select-All-Occurances when searching for something in a file. For example, in the layout created above, we can find ( Ctrl-F ) <EditText and press the Select-All-Occurances button. That will select all of the occurrences and we can navigate from there.

That’s it! A Small yet nifty feature that makes repetition less tedious.

Follow me on Medium if you’re interested in more informative and in-depth articles. Don’t forget to ask any questions, I’m always open to conversation.