Intellij IDEA Shortcuts and Tips

Intellij IDEA Shortcuts and Tips

Common Shortcuts

Operation Shortcut
Show intention actions(like quick fix) Alt + Enter
Code Completion Ctrl + Space
comment/uncomment a line Ctrl + /
To comment/uncomment block of code. Ctrl + Shift + /
Search Everywhere Double Shift
Go to class by name Ctrl + N
navigate to file Ctrl + Shift + N
delete current line Ctrl + Y
Debug Shift + F9
Run Shift + F10
Rename a variable Shift + F6

You can view or edit shortcuts in Settings -> Keymap

Source Code Hierarchy

  • Type hierarchies show parent and child classes of a class.
  • Method hierarchies show subclasses where the method overrides the selected one as well as superclasses or interfaces where the selected method gets overridden.
  • Call hierarchies show callers (supertypes) or callees (subtypes) of a method.

To show type hierarchy: select Navigate | Type Hierarchy or press Ctrl+H

To show method hierarchy: select Navigate | Method Hierarchy or press Ctrl+Shift+H

To show call hierarchy: select Navigate | Call Hierarchy or press Ctrl+Alt+H.

Import

To optimize Import, select Code | Optimize Imports.

To Enable Auto import

  1. Go to Settings
  2. Select Editor -> General -> Auto Import
  3. Check Add unambiguous imports on the fly and Optimize imports on the fly

Formatting

To Change Java Code Style, Open Preferences | Editor | Code Style | Java. Then import Intellij Code Style XML Schema.

You can import Google’s Intellij code style

In the menu select Code -> Reformat Code to format code.

Create vertical text block selection

  • Click Mouse wheel and drag down/up

Build Project automatically

  1. Go to Settings
  2. Select Build, Execution, Deployment -> Compiler
  3. Check Build project automatically

Git

By default Intellij use changelist to group changes not yet committed to VCS. You can switch to use Git Staging Area.

select the Enable staging area option on the Version Control | Git page of the IDE settings

Here is the commit tool window after the change

Change Error Highlight style

To chnage the highligh style for errors, go to Settings > Editor > Color Scheme > General

Live Template

Use live templates to insert common constructs into your code, such as loops, conditions, various declarations, or print statements.

To open Live Template, select Settings/Preferences | Editor | Live Templates

Useful Live Templates

  • sout - print a String to System.out
  • soutv - print a value to System.out
  • soutm - print the current class and method name to System.out
  • soutp - print parameter name and value
  • main - insert a main method, same as psvm
  • itar - iterate an array
  • iter - iterate iterable or array
  • itit - iterate java.util.iterator
  • fori - create a for loop
  • null - check if variable is null. if(expr == null) {}
  • nn - check if variable is not null. if(expr != null) {}

You can create your own template in Live Template tab. Example Template

1
2
3
private void $METHOD_NAME$($ARGS$) {
$END$
}

Example

1
2
3
4
5
6
@Bean
public CommandLineRunner applicationRunner() {
return args -> {
$END$
};
}

You need to set its context before you can use it. The above template’s context is “declaration”.

Run Anything

double-press Ctrl to bring up Run Anything window. You can use Run Anything for multiple actions:

  • Open recent projects
  • Launch run/debug configurations
  • Run build tool commands
  • Run CLI commands or scripts “in context”

see One Shortcut to Run Them All for more details

Reference