IntelliJ IDEA tips - mhulse/mhulse.github.io GitHub Wiki

See also: macOS Java Android dev setup (Android Studio, Intellij Community Edition, etc.)

Method Separators

In Preferences > Editor > General > Appearance, check the Show method separators box.

Change the separator color: See Preferences > Editor > Color Scheme > General > Code > Method separator color > Foreground

Draw lines between methods in code editor of IntelliJ

File Colors

Auto-generated files:

Preferences > Appearance & Behavior > File Colors

Ignored by VCS (e.g. .gitignore excluded files):

Test directory and files:

JetBrains Toolbox App

I recommend using the JetBrians Toolbox App to manage your JetBrains apps (like IntelliJ IDEA).

If not already using, you have to uninstall your existing version, and then install the toolbox. From there, you can choose JetBrains apps to install.

There’s also a project management tab, which makes it easy to open and switch between projects.

Tips

F4 contextual über jump to source (found under View menu)

CTRL + H for class hierarchy

COMMAND + SHIFT + A (search for hierarchy)

COMMAND + OPTION + arrow keys (square brackets may work too) to jump back and forth

Hold OPTION to insert cursor in multiple spots

When auto completing, hit TAB rather than ENTER (tab will replace rather than add)

OPTION + ENTER for contextual options based selected context

Visual guides

Preferences + Code Style + General > Visual Guides

I use the value 36,72,144, which is based on Python’s PEP 8 style guide’s “Maximum Line Length

Recent Files

CMD + E

Move forwards or backwards through caret history

CMD + OPTION + LEFT or RIGHT

Move caret to top or bottom of open file

You’ll have to remove the default CMD + UP and CMD + DOWN shortcuts.

See: Odd command + up arrow hotkey change

Wrap selection in tag

Note that by changing this mapped key, it will remove the existing (annoying) map of minimizing window to dock.

Make your selection and then: CMD + M

Searching for things

Specifically, files:

SHIFT + CMD + O

Files and folders:

Hit SHIFT twice and search for file or folder by typing its name.

Find and replace

Find:

SHIFT + CMD + F

Find and replace:

SHIFT + CMD + R

Navigating to Action menu

This is like Atom or VS Code’s “Command Palette”:

CMD + SHIFT + A

Bonus! If you’re on a Mac, disable these system shortcuts:

Killing test server process

If your Jetty/other server doesn’t shut down properly, and you run into conflicting ports, then run lsof -i:<port number>; once process is found, use kill -9 <process id>

Contextual options

Option + Enter for contextual options based selected context

Multi-select (like Atom/Sublime)

CTRL + G or CTRL + CMD + G to multi-select

Jump to definition

Hold down COMMAND to make whatever you’re hovering over clickable (if it’s a class, you can click to jump to that definition).

Copy smart

Copy class smartly using contextual menu and it will refactor the copied code contextually

Quickly navigate forwards and backwards

OPTION + COMMAND + LEFT ARROW OR RIGHT ARROW

General UI setup

zsh shell

If zsh has been installed via Homebrew: /usr/local/bin/zsh

Whitespace

If you don’t have an .editorconfig setup (see below) you might want to adjust these whitespace display/settings:

Autoscroll from source

When you open a file, reveal it in the sidebar too.

Alternatively, you can use: Navigate > Select in... > Project View

Sync settings to your JetBrains account

Enable this option via the File menu; login using your JetBrains account.

Note that you will have to select this option to login, and then select it again to enable it.

Better/larger editor font

Install Fira Code on your system.

  • IDE Settings > Editor > Font

Wrap selection in parens/curly braces/etc.

  • IDE Settings > Editor > General > Smart Keys > Surround selection on typing quote or brace

Gradle tool window

View -> Tool Windows -> Gradle

Note: Click the refresh icon in this tool window if you get a message like:

Warning: Unable to make the module: ap-site.main, related gradle configuration was not found. Please, re-import the Gradle project and try again.

This should be enabled by default:

Put a file named .editorconfig at the root of your project:

# This file is for unifying the coding style for different editors and IDEs.
# EditorConfig is AweSome: http://editorconfig.org/

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_size = 4
indent_style = space

Here’s a more complex example: @mhulse / gh-boiler / .editorconfig

Comments that obey indentation

File > Settings > Editor > Code Style > Java > Code Generation

Show Toolbar

If not done so already, go to View menu and choose Toolbar

Example Gradle “Import Project”

“Import Project” vs. “Open”

When you use “Import Project”, you are presented with a series of dialogs to set various options (for both Gradle and the project). Using “Open”, you do not get those dialogs as IDEA will use defaults (of which, you can manually change later).

I now prefer using an Application configuration vs. Gradle as it’s way faster! See “Application” vs. “Gradle” configurations for more info.

  1. From the Welcome window, choose “Import Project”.

  2. In the Finder window, navigate to your project’s root and click “Open”.

  3. From the “Import Project” window, choose “Import project from external model” and choose Gradle (see docs).

  4. Choose “Use gradle 'wrapper' task configuration”

    You can also specify how to group modules in your project. For example, if you want to sort out your modules and group them by their qualified names, use the using qualified names option. In this case when module groups are generated using Gradle group, the prefixes in module names are also accounted for.
    (See: Creating a new Gradle project)

    IntelliJ IDEA allows you to logically group modules. If you have a large project with multiple modules, grouping will make it easier to navigate through your project.
    (See: Group modules)

Next, IDEA will process your new project and open the IDE window.

Setting up configurations

“Application” vs. “Gradle” configurations

Q: What’s the difference between IntelliJ IDEA “Application” or “Gradle” configurations?

A: IntelliJ IDEA can either build and run the project itself or delegate these actions to Gradle. In some cases the project will build and run properly only with the delegation (in case of some complex configuration of your build scripts that IntelliJ IDEA doesn't understand and cannot convert to its internal build and run actions). So, it's up to you which way to use.

Gradle Goodness: Delegate Build And Run Actions To Gradle In IntelliJ IDEA

Setting up a configuration

  1. Click “Add Configuration”

“Application” configuration (preferred … It’s faster!)

  1. From the “Run/Debug Configurations” window, click the “Add New Configuration” plus icon in the top left of window; choose “Application”:

  2. Fill out the configuration like so:

    Note: Make sure your Gradle clean task is ran before the build!

    In older versions of IntelliJ, Make sure to click “Single instance only” (looks like this is now the default in newer versions of IntelliJ):

    Click “Share” too, if you want to include this config via git:

    The best way to share IntelliJ Run/Debug configurations between projects is to use the “Share” checkmark. The share check-mark pulls the setting out of your workspace.xml and instead puts it in the directory .idea\runConfigurations. This is designed so you can share the setting with others. You could copy this file and put it in the same location in all your idea projects.

  3. Click “Apply” and “OK”.

  4. Now you can run the project using the green “debug” or “run” icons:

“Gradle” configuration

  1. From the “Run/Debug Configurations” window, click the “Add New Configuration” plus icon in the top left of window; choose “Gradle”:

  2. Fill out the configuration like so:

    Make sure to click “Single instance only”:

    Click “Share” too, if you want to include this config via git:

  3. Click “Apply” and “OK”.

  4. Now you can run the project using the green “debug” or “run” icons:

⚠️ **GitHub.com Fallback** ⚠️