a shortcut that completely changed the way IĀ work.
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.
Multicursor toolkit. What do you really have to know to feel its trueĀ power?
The shortcutĀ itself.
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)
Selection shorcuts
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.
String Manipulation Android Studio plugin (optional)
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.
Real life usageĀ examples
Iām using Multicursor all the time. Sometimes for bigger tasks, sometimes for smaller ones. Here are some cases I used this shortcut in.
@IntDef
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):
Butterknifeās OnClick
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!