Although the list of shortcuts that IntelliJ offers is very large, here are just a few that make things so much easier when writing code (special thanks to the developers who pointed out some of them to me):
Ctrl + W
 several times to extend the selection around the cursor. Depending on how the code is formatted, the first use of the shortcut will select the word that is found at the cursor position. The second use of the shortcut extends the selection to the expression that contains the initially selected word, than you will select the line of code, then the paragraph, and so on.Ctrl + Alt + M
shortcut. Here, you can choose a name for the new method (if the suggested one is not of your liking), the visibility for the method (by default, IntelliJ suggests to make it private, but you can select another option from the dropdown) and you can declare it static if you want.After clicking the OK button in this screen, you will be shown the number of occurrences of the code you are extracting, within the current class. Now you can choose to replace the code with the call to the newly extracted method, or to leave it as is.
A very nice feature here is generating the new method with the correct signature – if, within the code that you want to extract, you are using some variables declared outside of the code snippet, IntelliJ will automatically create a parameter having the name of that particular variable within the method signature; hence, where you are replacing the code, instead of the repetitive code, you get the call to the new method, having automatically passed the variable on calling it. A quick example can be seen in the following code snippet:
Before using the shortcut:
|
After using the shortcut:
@Test public void newTest1() { String newString = "newString"; printAString(newString); } private void printAString(String newString) { |
To make this easy, the following shortcut can be used, after having selected the value that you want to extract:
Ctrl + Alt + F
. This shortcut will bring up a compact menu that will allow you to: choose from a suggested list of names for the new field (this will also generate the declaration of the new field), choose where to initialize it (in the current method, in the field declaration, in the constructor or in a setUp method) and choose to replace the occurrences of the field value throughout the class (the dialogue displays the number of occurrences found). If you do not want to use any of the suggested names for your new field, right after clicking the key combination described above (when there is a red border around the selected code), type the new name for the field and hit Enter.Ctrl + Alt + V
shortcut. When doing this, IntelliJ will declare it (having the name the same as the value it holds), initialize it with the value you provided, and prompt you to replace its’ occurence within the method (you can choose to either replace each occurence of the String with the new variable, or replace only the occurence you have selected when using the shortcut. You also have a checkbox for making it final, if you wish. An example can be seen below: Before:
@Test
public void newTest1() {
printAString("secondString");
printAString("secondString");
}
After
@Test
public void newTest1() {
String secondString = "secondString";
printAString(secondString);
printAString(secondString);
}
Ctrl + P
shortcut to show you what parameters having what types you must provide for the method, as you type them. If several method signatures exist, they are all displayed, with a highlight on the variant that most closely resembles what you are typing.
Alt + Insert
shortcut in IntelliJ. From the dropdown menu that appears you can choose to generate the getters or setters, or both, by performing a multiple selection on the values for which you want to generate them (hold Ctrl and click on the values you want).Alt + Insert
shortcut in IntelliJ, from where you can choose which constructor you want to generate (if you have multiple fields declared in your class, you can generate constructors for a number of these parameters, by performing a multiple selection on the values you are interested in).Ctrl + Shift + F7
shortcut after selecting the item you are interested in. All its’ occurrences will be displayed on a yellow background.soutv
right below the line of code that instantiates it. The output on the console will be ‘nameOfVariable = valueOfVariable’Ctrl + Y
will do the trick.Ctrl + Home
to jump to the beginning, or Ctrl + End
to jump to the end of the file.Ctrl + Alt + O
shortcut.Shift + Enter
shortcut. Shift + Shift
.Ctrl + Shift + N
.Ctrl + Alt + Shift + N
.Ctrl + Alt + L
shortcut either on a selection of code or without any selection (which means it applies to the whole class) to make everything fall into place.For the complete list of IntelliJ’s keyboard shortcuts please refer to this link.
Leave a comment