Skip to content

IntelliJ IDEA useful shortcuts and tips

Last Updated on 15/11/2020

Here follows a short list of some IntelliJ IDEA useful shortcuts and tips to improve your productivity whilst coding Java.

Code generation

Type Alt + Insert to open the code generation dropbox, then type:

Constructors

  • Enter directly, then Up and Ctrl + Enter for no-arg constructor.
  • Enter directly, then Ctrl + A to select all of the arguments.

Getters and Setters

  • get for generating only getters, then Ctrl + A to select’em all.
  • set for generating only setters, then Ctrl + A to select’em all.
  • gas for generating both getters and setters, then Ctrl + A to select’em all.

Override toString, equals and hashCode

  • to for generating the toString method.
  • ha or eq for equals and hashCode, then manually pick your fields or just hit Enter three times.

Snippets generation

Type the following inside of your class then press Enter to automatically generate the desired snippet:

  • psvm for a public static void main method.
  • sout for an empty System.out.println() call.

Shortcuts

  • Auto format: Ctrl + Alt + L.
  • Organise imports: Ctrl + Alt + O.
  • Introduce local variable: Ctrl + Alt + V.
  • Open type: Ctrl + N.
  • Open type exclusive to your project: Ctrl + Shift + N.
  • Search everywhere: 2x Shift, it opens a dialog where you can type things like format and the IDE will search for files and actions containing format in its name.

Execution

Running the main method

  • Ctrl + Shift + F9 to compile a single class.
  • Ctrl + Shift + F10 to run the main method.

Running the main method without compiling the whole project

When you run a main method of a class within your project, it invokes the Build process of the given project. To avoid that:

  1. Go to the dropdown menu at the left side of the green play button and select Edit Configurations...;
  2. On Application -> Class-you-want-to-run, remove the step Build at the bottom of the window by clicking on the red minus button and click OK;
  3. Back to your code, type Ctrl + Shift + F9 to compile it;
  4. Finally, type Ctrl + Shift + F10 to run your main method.

Code templates

If you still don’t use Lombok, the following templates are useful for auto generating code and javadocs. The highlighted lines are the lines I’ve changed from the original templates.

Getter method

#if($field.modifierStatic)
static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
#if ($field.boolean && $field.primitive)
  is##
#else
  get##
#end
${name}() {
  return $field.name;
}

Setter method

#set($paramName = $helper.getParamName($field, $project))
#if($field.modifierStatic)
static ##
#end
void set$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))($field.type $paramName) {
  #if ($field.name == $paramName)
    #if (!$field.modifierStatic)
      this.##
    #else
      $classname.##
    #end
  #end
  $field.name = $paramName;
}

I will keep updating this list as soon as I discover new features and shortcuts.
Cya!

Leave a Reply

Your email address will not be published. Required fields are marked *