A few useful IntelliJ keyboard shortcuts

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):

  • Select blocks of code: from where your cursor is, use 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.
  • Extracting a method from repetitive code: when you realize that your class contains a chunk of code that repeats itself throughout the class, a very easy way to extract the code into a separate method and replace its’ occurrences with the new method is to use the 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.
    newMEthod1
    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.
    newMethod2
    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:


    @Test

    public void newTest1() {

    String newString = "newString";

    System.out.println("newString = " + newString);

    }

    After using the shortcut:

    @Test

    public void newTest1() {

    String newString = "newString";

    printAString(newString);

    }

    private void printAString(String newString) {

    System.out.println("newString = " + newString);

    }

  • Extracting a field: Suppose that throughout your class you are using a parameter of the same value, that can be extracted as a field (for example the integer value 10, like in the screenshot below). This value might change in time, and you would like to hold this value in a field of your class, so that you have easy access to modify it when needed.
    variableBefore 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.
    fieldAfter
  • Extracting a variable:When you have a property that you use several times in a method, for example a String,by just typing in its’ value, it would be very useful to declare it as a method variable. This can be easily done by using the 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);

    }

  • Displaying a method signature: When calling a method that needs to be provided parameters to comply with its’ signature, you can use the 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.
  • Generating getters and setters: Whenever you deal with a large number of properties that need to be declared in a class, manually writing all the corresponding getters and setters is cumbersome and error prone. Therefore, you can use the 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).
  • Generating constructors: When you don’t want to write a classes’ constructor manually, you can use the 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).
  • Highlight usages in current file: When you want to easily see where a method, field or parameter is used within the current file, you can use the Ctrl + Shift + F7 shortcut after selecting the item you are interested in. All its’ occurrences will be displayed on a yellow background.
  • System.out.println of a variable: If you have a variable whose value you want to print, just type soutv right below the line of code that instantiates it. The output on the console will be ‘nameOfVariable = valueOfVariable’
  • Delete current line: A simple Ctrl + Y will do the trick.
  • Jump to beginning or end of file: Use Ctrl + Home to jump to the beginning, or Ctrl + End to jump to the end of the file.
  • Optimize imports: When you don’t want to manually remove unused imports or group imports of methods from the same class, you could use the Ctrl + Alt + O shortcut.
  • Create new line: When your cursor is found on a line of code, after which you want to insert a new line and start typing directly in it, without having to manually move the cursor to the starting position, just use the Shift + Enter shortcut.
  • Find a file, class, or method in the whole project: Simply use Shift + Shift.
  • Find a file or class in the whole project: Simply use Ctrl + Shift + N.
  • Find a method in the whole project: Simply use Ctrl + Alt + Shift + N.
  • Reformat code: If your code’s indentation is not optimal, you can use the 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.
    1. For the complete list of IntelliJ’s keyboard shortcuts please refer to this link.

      Advertisements

      Leave a Reply

      Fill in your details below or click an icon to log in:

      WordPress.com Logo

      You are commenting using your WordPress.com account. Log Out / Change )

      Twitter picture

      You are commenting using your Twitter account. Log Out / Change )

      Facebook photo

      You are commenting using your Facebook account. Log Out / Change )

      Google+ photo

      You are commenting using your Google+ account. Log Out / Change )

      Connecting to %s