It all started with the video:
(you don’t really need to watch it to understand this post, I will explain everything you need to know, although if some day you find some time for it, I would highly suggest you do, there’s a lot of awesome stuff there)
As soon as I got to about 7:23 and I started listening about Android Studio feature I have never heard before, I became really, really intrigued.
Multicursor.
CTRL + G (ALT + J for Windows/Linux).
It was such a nifty feature, such a cool think to have at your hand at all times. Immediately I thought:
Boy, am I getting some more monkey-jobs copying code. I will click the hell out of my CTRL and G buttons.
At that moment I did not even consider it could change my coding style so much.
CTRL+G. If you use it while having a particular text selected, it will select the next occurrence of this text in your code (creating a new cursor).
CTRL + G
The important part is, the Multicursor feature perceives your code just as a text, so you can also select a java-specific keywords, and build your Multicursor based on that.
CTRL + G (on the case keyword)
Three shortcuts to learn here:
(I could try to explain what they do but it’d be the best if you just tried them… seriously, it’ll take you like 30 seconds… at most)
IMPORTANT: I also use them without SHIFT just to jump around the code.
This one is not really required, but it can speed up your work significantly.
I find myself using mostly the Create Sequence feature, but there’s much more you can utilize on a daily basis. An awesome plugin to have installed in your Android Studio.
I’m using Multicursor all the time. Sometimes for bigger tasks, sometimes for smaller ones. Here are some cases I used this shortcut in.
I’m a huge fan of Support Annotations. I use them all the time. With the @IntDef at my fingertips, I sometimes give up on using enum, and just grab a handful of ints instead. Take a look at the way Multicursor can help me with that.
Here’s where I wanna get:
Here’s how I can get there:
Setup:
Just a regular copy-pasting
And renaming
Multicursor:
CTRL + G
I could’ve used it on multiple different things (“=”, “1”, “;” or some sort of set of those), I chose “int” for some unknown reason.
Creating sequence:
If you don’t want to use String Manipulation plugin you can do that manually. Here’s with the plugin:
The last could’ve been just SHIFT + ← as well
(I usually just click C and 5 in the plugin menu, here using mouse for better readability)
Copying modified version of rows:
And here’s where the true magic happens:
And the @IntDef is up and running. Done.
Extracting style from layout xml
Multicursor can easily work in layout xml as well. My exact case was a bit more complicated but the main issue was the same. I decided to extract a style from few Views in layout xml file.
In this example I used Multicursor twice. In two different ways.
Let’s assume this is the layout:
That’s how Multicursor can help me with the extraction process:
And that’s how I apply the extracted style to the xml (also with the Multicursor):
This example is something that happened to me few days ago.
I had 5 buttons in the compound View I was working on, and I added them to the class using another Android Studio plugin (that generates @Bind statements). But then I decided that I don’t really need to store references to those Buttons. I just needed to handle the OnClickListener. This is how I handled the change with the help of Multicursor:
Quick and easy.
And many many more…
And those are just three simple examples. I also had to “copy” a ton of (~3k lines of code) Swift code into Java. It was significantly less painful with the multicursor feature. I did it about 2 weeks ago and I would probably be still doing it if there was no multicursor feature.
For me it started as a cool little trick that I thought I would probably use once or twice. But as with most new things you learn, as you work along you find more and more usages for it, and all of a sudden you can’t believe how you could have lived without it before.
If you’re not using it yet, I highly suggest you try!