IntelliJ IDEA Java Tips & Tricks

Editor Tips and Tricks in IntelliJ IDEA

IntelliJ IDEA offers you a huge range of keyboard shortcuts to help you get faster and be more productive in Intellij IDEA. These shortcuts can also be configured if required.

This blog post covers the same material as the video. This provides an easy way for people to skim the content quickly if they prefer reading to watching, and to give the reader/watcher code samples and links to additional information.

IntelliJ IDEA has lots of keyboard shortcuts and features that help you write code. In this tutorial we’re going to see some shortcuts that you can use to make it easier to write and edit code.

Focusing Our Workspace

The most useful shortcut to help you focus on your code is ⇧⌘F12 on macOS, or Ctrl+Shift+F12 on Windows and Linux. This hides all the other windows so the editor takes the full space available. It’s worth using this shortcut when you start working on something to minimise distractions and maximise space.

IntelliJ Full Screen

Editor Shortcuts

There are several editor shortcuts that you can use to speed up your workflows, for example, ⌘⌫, (or Ctrl+Y), to delete the current line.

To cut a line, press ⌘X, or Ctrl+X, without selecting or highlighting anything. This saves a few keystrokes. We can paste this line using ⌘V, or Ctrl+V, like in any application.

Like cut, we can copy a whole line with ⌘C, or Ctrl+C, without having to highlight it first, and paste this too. Sometimes it might be more useful to duplicate the line instead with ⌘D, or Ctrl+D.

Try it out with the code below:

public class MultiLineStrings {
    public void multiLineStrings() {
        // Pressing Command+X or CTRL+X will cut the line
        // Pressing Command+V or CTRL+V will paste the line
        // Pressing Command+C or CTRL+C will copy the line
        // Pressing Command+D or CTRL+D will duplicate the line
        String someText = "This code can be on a single line " +
                "or it can be on multiple lines " +
                "and you can type and hit Enter and get the + sign " +
                "inserted automatically";
    }
}

Moving Code

To move a statement up use ⇧⌘↑, or Ctrl+Shift+Up Arrow. Moving a statement moves the Java statement as a whole. Applying this to the line with the variable declaration will move the whole statement, in this case the whole multi-line string declaration. The move is context sensitive, so in this case the IDE won’t let us move it out of the method. Use ⇧⌘↓, or Ctrl+Shift+Down Arrow, to move it down

Animated GIF showing how to move statements up and down

If you want to move just a line, or move something without factoring in code context, you can move a line up with ⌥⇧↑, or Alt+Shift+Up Arrow. This means you can move the line anywhere. If we select more than one line and use the shortcut, it will move all highlighted lines.

Animated GIF showing how to move lines up and down

Manipulating Strings

One of the simplest but most useful editor features is creating a line break in the middle of a declared String using Enter. IntelliJ IDEA will automatically close off the String on the line above, add the ‘+’ symbol for concatenation, and reopen the String on the line below. Try it out with the code below, press Enter somewhere inside the String value:

public class MultiLineStrings {
    public void multiLineStrings() {

        // Ctrl+Shift+J will join lines
        // Pressing Enter in String will auto-insert +
        String someText = "This code can be on a single line " +
                "or it can be on multiple lines " +
                "and you can type and hit Enter and get the + sign " +
                "inserted automatically";
    }
}

To join the lines together, use ⌃⇧J, or Ctrl+Shift+J. This is extremely helpful for formatting multiline strings, and for laying out block comments.

As well as pressing , or Enter, to split the line and put the caret on the new line, you can also use ⌘⏎, or Ctrl+Enter, to split the line but keep the caret on the current line.

Selecting Code

You can use ⌥↑, or Ctrl+W, to extend your selection to include ever-increasing, valid blocks of code. Use ⌥↓, or Ctrl+Shift+W, to decrease the selection back to where you started.

Animated GIF showing how to expand and shrink chunks of code

Working with Linear Formats

Column Selection Mode is a useful tool for manipulating code or text and working with linear formats. You can search for it in two ways:

  • Find Action ⌘⇧A, or CTRL+SHIFT+A
  • Search Everywhere ⇧⇧, or SHIFT+SHIFT

Then type in Column Select.

Alternatively, use the shortcut ⇧⌘8, or Shift+Alt+Ins. Once you’re in column selection mode, you can hold down shift and press up or down to create carets in a column above or below where you are. The same shortcut toggles off Column Selection Mode.

Animated GIF showing how to expand and shrink chunks of code

Manipulating Caret Placement

To place multiple carets in specific places, hold down ⌥⇧, or Alt+Shift, and use the mouse to click where you want the next caret to be. As you type, each caret will output the same thing. This might be useful when making multiple changes to XML files, for example. To get back to your original caret, press Escape.

Animated GIF showing how to use and place multiple carets

To clone the caret above or below the line you are on, press , or Alt, and then , or Alt, a second time, but this time keep holding down , or Alt. Press down to create another caret directly below the line. Again, this is useful for column based formatting or edits. Press Escape to go back to one caret. Similarly, press , or Alt twice, holding down the , or Alt on the second time and press the , or Up Arrow, to clone the caret to the line above.

Finding a Word in the File

As well as using ⌘F, or Ctrl F, to find something, you can use ⌘G, or F3, to find the next occurrence of the word currently at the caret, and move to it. Alternatively, use ⌃⌘G, or Ctrl+Alt+Shift+J to select all instances of the current word.

Animated GIF showing how to select all instances of the same word

Select and Surround

To add quotes around a word or words, highlight the text and type the quote. This also works for all types of brackets. The quotes or brackets will be placed directly outside the highlighted text.

Animated GIF showing how to wrap words with quotes and brackets

IntelliJ IDEA Clipboard

Finally, IntelliJ IDEA keeps a record of everything that was copied inside the IDE. To paste an item that was copied in IntelliJ IDEA in the past, use ⇧⌘V, or Ctrl+Shift+V. All the items copied or cut in the past are shown and we can use the keyboard to navigate them. We can even copy sections of the copied code for later use. You might notice from this example that the editor and other sections of IntelliJ IDEA support emojis on macOS.
Clipboard in IntelliJ
You don’t have to use the arrow keys to move all the way down to the item you want in the History window. Just type the number next to the history item to paste, for example number 1.

That’s a small selection of shortcuts to help you write code, or text, in IntelliJ IDEA.

See also:

Thanks for reading!

image description