It’s not secret that my favourite IDE is IntelliJ IDEA. You have already seen me using it in my previous videos, so it was just a matter of time to bring it as a topic. Here it is! Not trying to convince anyone to change or to start an IDE flame war. It’s enough already with the Java EE vs Spring wars.
Anyway, in a bit longer than 5 minutes this time, I went through some of the features that I use the most in IntelliJ, on Navigation and Code Generation.
Here is the video:
Here are also some other posts I’ve wrote about IntelliJ:
Are you looking for ways to be more productive? It shouldn’t be a secret that performing actions using the keyboard instead of the mouse will save you time. If you think only about a single action it’s not a big deal. What if you use the same action multiple times a day? If you add up all these actions, they can have a great impact on your productivity.
I’m more or less used to drive most of my actions with keyboard shortcuts. When I was younger, I played semi-professionally Real Time Strategy computer games, including Starcraft and Warcraft III. Starcraft, popularized the term APM (Actions per Minute), which counted the number of actions that a player performed per minute. By using tools, it was possible to record APMs and tell if players were using mouse actions or a keyboard and mouse action combination. Usually, players with a keyboard and mouse combination gameplay had a better chance of winning games than the one that just clicked.
So, what does this have to do with code and IntelliJ? Well, I believe that you can increase your code development productivity by learning and using the keyboard shortcuts to perform the desired actions. You can check the keyboard shortcuts on IntelliJ and you can also check the Productivity Guide which monitors your most used actions. This information is very useful, but it may be a little difficult to change your habits right away. To help you with this, I will describe my most used shortcuts in IntelliJ. You can start familiarize yourself with these and slowly introduce additional shortcuts.
Shortcut
Description
CTRL + W / CMD + W
Syntax Aware Selection
This allows you to select code with context. Awesome when you need to select large blocks or just specific parts of a piece of code. If you have this code:
And place the cursor in the auctionFile and use the shortcut, it will select auctionFile. Press it again and the selection will expand to auctionFile -> createAuctionFile(realm, auctionFile). If you press it again, now the selection will expand to files.getFiles().forEach(auctionFile -> createAuctionFile(realm, auctionFile)). Pressing a final time, you get the full piece of code selected.
If you combine it with SHIFT, you can unselect by context as well.
CTRL + E / CMD + E
Recent Viewed Files
This will show you a popup with all the recent files that you have opened in the IDE. If you start typing, you can filter the files.
CTRL + SHIFT + E / CMD + SHIFT + E
Recent Edited Files
Same as Recent Viewed Files, but only shows you the files that you’ve actually changed.
This will try to complete your current statement. How? By adding curly braces, or semicolon and line change. For instance, if you have the following statement:
System.out.print()
Press the shortcut once to add an ending semi-colon. Press it again to add a new line and to position the cursor aligned with the last line.
Another example: if (condition == true)
Press the shortcut to add opening and closing curly braces, and place the cursor inside the if body with additional indentation.
This one allows you to search by name for a Java file in your project. If you combine it with SHIFT, it searches any file. Adding ALT on top of that it searches for symbols. In the search area, you can use CamelHumps (type the capital letters only of the class name) notation to filter files.
I didn’t mention it before, but I guess you are familiar with auto complete via CTRL + SPACE or CMD + SPACE. If you add a SHIFT you get the smart completion. This means that the IDE will try to match expected types that suit the current context and filter all the other options.
CTRL + ALT + ← / CMD + ALT + ←
Navigate Back
This allows you to navigate back like a browser action. It remembers where your cursor was positioned and navigates back even to other files.
Place the cursor in a element and after pressing the cursor the IDE will highlight all the occurrences of the selected element.
There are many more keyboard shortcuts. Almost every action has an equivalent shortcut. It’s hard to learn them all, it takes time and practice. I still learn new things every week, and if for some reason I don’t code as much for a few days, I forget about the new shortcuts I’ve learned. It’s practice, practice, practice! Try to learn a few and master them instead of trying to do everything in one go. It’s easier!
An IntelliJ plugin exists to tell you with shortcuts you should use if you use the mouse. Its Key Promoter, but unfortunately it seems it’s not maintained anymore. Maybe I can update it for the latests IntelliJ versions. I would also like to see in the Productivity Guide a count of actions performed by shortcuts or mouse. If I find some free time, maybe I can do it too.
I have been a long time user (and customer) of IntelliJ IDEA. I think I have started using it around 2005 or 2006, version 5.0 at the time. I was an Eclipse user back then. A few of my colleagues recommended it to me, and at first I was not convinced, but after trying it out I was impressed.
Now in 2014, IntelliJ IDEA is still my IDE of choice. The intent of this post is not to start an IDE war, but to focus on a few of IDEA features that sometimes other IDEA users are not aware of.
Darcula Theme
The Darcula Theme changes your user interface to a dark look and feel. Well, maybe this is nothing new for you, but I would like to point two major advantages. First, it causes much less stress to your eyes. Give it a try! After a few hours using the dark look if you switch to the default one again you’re probably going to feel your eyes burning for a few minutes. Second, if you’re a mobility addict and you’re always running on battery, the dark look can also help your battery to last longer.
Postfix completion
Postfix completion is the feature that I always wanted and I didn’t even know it. Postfix completion allows you to change already typed expressions. How many times all of us have cursed for having to return back to add a missing cast? Or because we actually wanted to System.out the expression? Well, Postfix completion fixes that.
For instance for the System.out, you type the expression:
someVar
You can now type:
someVar.sout
And the expression is transformed to:
System.out.println(someVar);
Check this awesome post in IntelliJ Blog for additional information about Postfix completion.
Frameworks and Technologies Support
In the Java world, you have a lot of frameworks and technologies available. Most likely you will come across to many of them in your developer work. Sometimes, it’s a nightmare to deal with the additional layer and the required configuration for everything to work correctly.
Look at Maven for instance, it’s a pain to find which dependency to import when you need a class. IDEA Maven support, allows you to search for the class in your local repository and add the correct dependency to your pom.xml file.
Just type the name of the class, press Alt + Enter and Add Maven Dependency:
Pick the library you need. It’s added automatically to your pom.xml.
You have support for Java EE, Spring, GWT, Maven and many others. Check here for a full list.
Inject Language
With Inject Language, it’s possible to have syntax, error highlighting and code completion for a large number of languages into String literals. I use GWT a lot, and this allows me to be able to write safe HTML into the String HTML parameters of the API, like this:
Other examples include, SQL, CSS, Javascript, Groovy, Scala and many others. Try it out by yourself by pressing Alt + Enter on a String statement and then Inject Language.
Presentation Mode
Did you ever had the need to make a presentation about code using your IDE and the audience is not able to see it properly due to font size? And then you have to interrupt your presentation to adjust it. Sometimes you don’t even remember where to adjust it. Wouldn’t be easier to just have a dedicate presentation mode? Just go to View menu and then Enter Presentation Mode option.
Conclusion
I do believe that choosing an IDE is a matter of personal preference and you should stick with the one you feel more productive for the task that you have to complete. I still use Eclipse when I have to deal with BPM stuff.
Some of these features also exist on the other IDE’s, but I have the impression by chatting with other developers that they don’t know about their existence. Explore your development environment and I’m pretty sure you will learn something new. I’m always learning new stuff in IntelliJ IDEA.
Missing a feature you love? Feel free to post it in the comments section!
Do you find yourself writing the same code over and over again? Maybe you’re already familiar with IntelliJ Live Templates feature. If that’s the case don’t go away. I’m going to demonstrate how to use them, but also the most interesting part is how to create your own Live Templates that can help you on your everyday coding.
For those not familiar with Live Templates, this feature allows you to quickly insert code fragments in your source code files and have the ability to replace or auto-complete sections of the template based on the current code context. IntelliJ already has a lot of templates that you can use right away after installing the IDE. You can have a look into the templates by using the shortcut CTRL+J/CMD+J:
You get a list of the Live Templates that can be applied in the current context. The context is related with the file type you are editing (Java, SQL, XML, HTML, etc.), and also with the section itself (inside a method, field declaration, in the comments, etc). Let’s try one of the Live Templates I use the most, iterate an array. Did you notice the abbreviation on the left side of the Live Template popup? You can use it instead of using the popup shortcut. Just type iter followed by TAB key (do it inside the main method):
The Live Template gets expanded to the Java 5 for each loop and IntelliJ suggests you the args array from the main method in the iteration plus the element type being iterated. You can pick other arrays available in the context and the code will automatically adjust to your choice (look at the red square around the args, it can auto complete to other arrays using CTRL+SPACE). Pretty cool!
Other notable Live Templates that I find pretty useful:
Abbreviation
Generated Code
psvm
public static void main(String[] args) {}
psfs
public static final String
psf
public static final
sout
System.out.println();
soutv
System.out.println("var = " + $var);
St
String
ifn
if ($var == null) {}
inn
if ($var != null) {}
Try to explore others that suit your programming style. Remember, Live Templates change with the file type you are editing. If you type udp in a SQL file and press TAB the abbreviation gets expanded to update set = where ;.
The real power is IntelliJ ability to create and customise your own code fragments. Lacking ideas? How about List<String> strings = new ArrayList<String>();? You type that a lot right? Well, maybe not with the String type, but that’s just an example. We’re going to create a Live Template for it where you can pick the type of the elements in the List. Go to IntelliJ Settings CTRL+ALT+S/CMD+, and search for Live Templates. You should get the following screen:
We can now add a new Live Template by pressing the + on the right upper corner of the screen. We now need to add an abbreviation, a description, the template text itself and pick the applicable contexts.
Property
Value
Abbreviation
ls
Description
List
Template Text
List<$TYPE$> $VAR$ = new ArrayList<$TYPE$>();
Applicable
Java: statement, declaration
The $TYPE$ and $VAR$ are variables that you can replace on the Live Template generation using IntelliJ pre-defined functions. Go to Edit Variables screen and you should get a screen like this:
So expectedType() replaces the $TYPE$ variable with a class type with the default value of String and suggestVariableName() replace the $VAR$ variable with an auto suggestion for the variable name where the default value is list. Take a look into IntelliJ help and see which other predefined functions are available for you to use. You can even use Groovy code!
Now you can type ls in the editor and expand it with TAB. You will get:
You can auto-complete and replace String by another type. Notice how the rest of the template adjusts to your pick:
Note: If you’re using Java 7 or higher you can get rid of the generic type declaration on the right hand side of the template. I’m still working in a couple of projects that use Java 6, so I need it this way.
Now you just have to think on the code you type all the time and convert it to a Live Template and save precious time typing it! How about a template to create the Log instance to perform logging? You’re also going to need template log methods like sout to output log.info(""), log.error("") and so on. Try them out!