Useful Shortcuts To Code In IntelliJ Without Mouse(mac)

JAVING
Javarevisited
Published in
12 min readDec 30, 2021

--

It’s really infuriating when the mac mouse runs out of battery specially if you are fixing an urgent bug.

I think somebody once explained me how the apple doesn’t want developers to burnout and encourages them to take a break now and then and for that reason they placed the charging socket under the mouse. Honestly I don’t know what to think about that. I am definitely not a track pad developer, I love coding with mouse and keyboard.

So when the other week I was in the middle of fixing some bug and the mac mouse decided to run out of battery, I was not really worried because I am a software craftsman and I can use the IDE without a mouse if I need to.

In this article I am going to share some essential and some advanced shortcuts for IntelliJ mac users, so they don’t have to suffer anymore with the track pad when the mouse is not available.

I will use this notation for special keys:
⌃: Control
: Option
⌘: Command
⇧: Shift

Jump left and right
⌥ + →
and ⌥ + ←
When reading a long line of code that contains punctuations or brackets and we use the mouse to get to a particular location in the line, it can happen that sometimes miss the exact line we want to be or even choose the wrong line. But if we use the jump shortcuts ⌥ + → and ⌥ + ← we can faster jump from one punctuation sign to another. Also we can release the key as we get closer to the desired location and just use and for more precision.

In the above images an example of how using the jump shortcut a few times we can quickly advance to nearly the middle of the line. If we were just to use the arrow, it would took us ages.

Gradual Selection
⌥ + ↑ and ⌥ + ↓
The gradual selection shortcut is an excellent tool to gradually choose the parts of a statement. It is really useful when doing refactoring such as extract method. See what happens when I use a few times in a row.

Gradual occurrence selection
⌃ + G

Sometimes you want to select all occurrences of characters that are located in different lines but you want to do it gradually rather than selecting them at once straight away. This shortcut does exactly that.

Column Selection Mode
⌘ + ⇧
+ 8
This command toggles column selection on and off. What this does, it allows the to create multiple carets vertically using the arrows ⇧ + ↑ and ⇧ + ↓. Very useful when working with files that contain large amount of duplication and need to be refactored. Let’s have a look at an example.

Imagine this file has a few hundreds of lines similar to this one. But for some reason those first 6 lines are written in the wrong format. They should always be lower case. So to fix this, first we place our caret on the first line, as shown in the image below.

Then we toggle the column selection mode using ⌘ + ⇧ + 8. We will know that is enable because at the right bottom edge of the editor it will say the word Column

Now that the toggle is on, we use + until the we have a caret in front of each of the lines we want to modify.

Next we release the key and we use ⌥ + → to jump once to the right.

Now hold ⇧ + → until all the uppercase characters are selected and transform them to lower case with ⌘ + ⇧ + U

Once finish just press again ⌘ + ⇧ + 8 to disable column mode

Selective Caret Placement Mode
⌥ + ⇧ + click
The previous shortcut is really useful but there are situations where we need even a higher level of precision and we want to add the carets in particular locations. For such situations we can just use ⌥ + ⇧ + click for selective caret placing. See this image below I used just on the lines that I am interested in modifying.

From here is the same as in the previous shortcut but notice that IntelliJ for this one doesn’t to say that is Column mode, it says that is caret mode and it counts the placed carets.

Move line up and down
⌥ + ⇧ + ↑ and ⌥ + ⇧ +
This allows us to move a line of code a row up or a row down at a time

Move method up and down
⌘ + ⇧ + ↑ and ⌘ + ⇧ +
This allows us to move a full method up or down;

Find class
⌘ + O
Useful to quickly find a class

Find a file
⌘ + ⇧ + O
This one is extremely useful if we are looking for a file that is not a java class. I love that IntelliJ’s search uses a smart match to find in case you just partially remember the name of the file.

Find Anything
⇧⇧
Such a simple command but many don’t know is just to double tap shift. Automatically it will search in the All tab for anything that matches. It could be a class, a file, a config setting in the editor etc.. Easy to access and very powerful.

Global String Search
⌘ + ⇧
+ F
This is a shortcut you can’t live without. Quick and easy you can search for occurrences of any string you want in the whole project. You can also trim the scope using the left and right arrows. Once in this view you can navigate with and ↓, also you can change scope with any of ⌥ + P, ⌥ + M, ⌥ + D or ⌥ + S. To select just press ⏎.

Scratch file
⌘ + ⇧
+ N
In many occasions we need just a little scratch file of a particular type to play with, write some example code or just some reminders even. I often use the plaintext, json, sql and other scratch files to do little experiments so I don’t break the compiler while I experiment. When the shortcut is pressed a popup will appear prompting to select the type of file. There are lots, we can scroll to select.

If you don’t want to scroll, just start typing the name of the extension and the search feature will show it first.

Spy into methods and classes
⌥ + space
This shortcut is useful for exploring methods and classes without having to jump into them. If we decide that we want to jump into then we press ⏎. This is shortcut is excellent because allows us to remain in our current location while exploring other methods or classes. If we didn’t have this shortcut the only shortcut we would have to explore the internals would be to jump into directly using ⌘ + B.

See Available options / Autocomplete
⌃ + space
This shortcut is a basic one and a must know. When we place our caret next to a dot and trigger the shortcut it will show us the possible methods that we could use.

We will then press to select.

Another option that we have is to replace. Let’s say we changed our mind and we realised that we want to call discountApples() instead of discountBread(). What we will do is go back to the dot and press ⇥ to replace.

Auto suggest
⌥ + ⏎
This shortcut behaves different depending on where do we use it. If we use it in an access modifier it maybe gives us different suggestions that if it we use it on a local variable or in a parameter.

Code Generation
⌃ + ⏎
The code generation command can help us auto generate some useful code. But I must admit I don’t use it so frequently since most projects nowadays use lombok, so there’s no need for this kind of boilerplate most of the times.

Recent Files
⌘ + E
If we like working without mouse, we will need to get dexterity with the usage of tabs in the editor. In this next section I am going to talk about some of the shortcuts that will allow us to efficiently use the editor tabs without having to move the mouse. Recent Files is the first once on my list, I use this one a lot, no need to scroll the whole project navigation menu again if you closed something by mistake, you can just check the most recent files with this shortcut.

Nav Bar
⌘ + ↑
If you want to work without mouse probably this is one of the most useful navigation commands. Using the key combination ⌘ + ↑ will enable the navigation mode in the Nav Bar.

Once the navigation mode is enabled, using the arrows , , , will allow you to navigate classes, packages, files and even methods. When you select the location you want to go to, then press ⏎.
If you changed your mind and you want to exit navigation mode without selecting anything, just press esc and it will take you back to the normal mode.

Project view toggle
⌘ + 1
If we are observant, IntelliJ will tell us about this shortcut when everything is close. It’s handy sometimes to open the project view and observe the packages layout visually. When the focus is in the project view, we can navigate it using , , ,←. Also if we want to expand or collapse all directories we can do it with ⌘ + ‘+’ and ⌘ + ‘-’.

Switcher
⌃ + tab
and ⌃ + ⇧ + tab
With this two shortcuts we can switch between the open windows. Also we can activate some useful menus.

Next Tab and Previous Tab
⇧+⌘+]
and ⇧+⌘+[
This shortcut is essential, it allows quickly navigation between open tabs left and right. See in the images below how the tabs switch when we use it.

Delete line
⌘+ X
Using this shortcut is possible to delete a row of code.

Duplicate line
⌘+ D
Using this shortcut is possible to duplicate a row of code.

Close tab
⌘+W
Another must know. Just on the active tab at any moment press this shortcut and the tab will close. See this example where I closed a couple of tabs.

⌃ +⌥ + O
Remove unused imports
Very useful, I often use this one to tidy up. It will remove any unused imports in the current class.

⌘+⌥ + L
Format code
Will format the code and fix the indentations. I use it frequently when tidying up.

Split windows
⇧⇧ + (type split in the search and use arrows to choose)
Last but not least, window splitting. This feature is very useful. The way I often do it is by double tapping and then finding the split option I want.

If you choose split and move, it will move the tab to a new window. But If you Just use split, it will duplicate it. Here Screenshots showing the difference:

One last thing to mention is how to focus on either the left or right window. For that use ⌥ + tab. If you want to create any of the tabs you already know the shortcut(⌘+W). If you want to close all the tabs in a window group that’s also possible but I forgot how to do it, I think that in newer versions of IntelliJ that shortcut is unbound because if you focus click on any tab you will see that there’s no shortcut next to it like there is for close:

Also I don’t know how what is the shortcut to be used instead of focus click to display that menu on an active tab. If you know, please let me know in the comments.

In this post I talked about the shortcuts that allow me to work without a mouse in IntelliJ idea. I personally feel that this shortcuts allow me to work within the IDE without a mouse for at least 90% of the tasks I need to do on daily basis. Of course there are many more shortcuts and tips but I hope you found this ones useful. p.s One of the best ways to practice coding without mouse is to do TDD katas, I invite you to give it a try.

--

--

JAVING
Javarevisited

The present continuous form of “to program in Java”.