IntelliJ Tips

IntelliJ Tips

Introduction

Hi, this is Trueman, an Application Engineer working at Rakuten's Osaka Branch. When i first started using IntelliJ in school for programming assignments, it was little more than a glorified text editor that allowed me to run my code a bit more conveniently. Eventually during my first internship as a junior developer, I found out about all sorts of neat tricks to really take advantage of the capabilities of a full-blown IDE. I definitely would've appreciated learning about a lot of these shortcuts sooner so if you're just starting out, hopefully these can help you out! And even if you've been a professional dev for a long time now, maybe there's still a hidden gem or two in here you can make use of as well!

Just a quick disclaimer: the shortcuts I'll be sharing are based on IntelliJ2021 Ultimate Edition on a Mac OS. I'm sure a lot of the shortcuts won't have changed much between IntelliJ versions and that there's very similar equivalents for a Windows OS but keep in mind some things might be a bit different for you. Also, although I haven't tried it myself, very likely, a lot of these will work with other JetBrains products as well (like Webstorm, CLion, PyCharm, etc) and other non-jetbrains IDE's probably have some sort of equivalent, though probably with a very different set of shortcuts.

Anyways I'll be listing out the IntelliJ shortcuts/features below grouped by theme, and ordered with most frequently used ones near the top of each list. The details and explanations might get a bit long so if you just want a quick reference, just look at the table of contents up above.

Navigation

1. Jump Everywhere / See all References (Cmd + Left Click)

Once you get past the point of writing single page scripts with less than 100 lines, you're probably going need to jump all around your code base to follow the flow of your program. This Cmd+Click shortcut is going to be the single most useful feature of IntelliJ you're likely to use so if you're only going to remember one thing from this article, remember Cmd + Left Click! This useful in so many circumstances:

You want to see where a variable is declared? Cmd+click it.

(the cursor jumps directly to where the variable is declared after clicking on it)

You want to see where a variable is used? Find where it's declared and Cmd+click it.

You want to know what that function does? Cmd+click it.

You want to see the class definition of the function call's return type? Cmd+click it.

You want to see a list of all of the callers of this helper function? That's right, cmd+click it!

2. Jump Back/Forward (Mouse's back and forward button or Cmd + [  and Cmd + ])

Often when you're jumping around your code base, you'll want to jump back and forth to check on different things. Maybe you jumped a little bit too far, or you hit a dead in following function calls all the way down, or you want to explore another branch of the control flow. It's during times like these that a slightly fancier mouse with back and forward buttons (the same buttons that trigger your web browser's "go back to previous page" and "go forward to next page" behaviours) really shines.

Just hit the back button and you'll be right back to where you were at before the last jump (and feel free to hit the back buttons a few more times if you need to go back even further). If you went back too far, just hit the "forward" button to go "back" to where you were at before you went back (if that makes any sense).

Of course, if you're using a boring mouse with just left and right click (and maybe a mouse wheel), or just using a trackpad, then you'll have to make do with the keyboard shortcuts: "Cmd + [" to go back and "Cmd + ]" to go forward.

 3. Search Everywhere (Double Shift)

Being able to jump around by using "Cmd + click" is of course very useful for navigating around your code. But what if you're interested in a class or method that's in a whole other part of the code base from where you currently are? No problem, just hit the shift key twice and start typing in the name of the class or method you want to jump to (or paste the thing in the search box), and click the link.

4. Find in Files (Cmd + Shift + F)

Searching for classes and method names works really well with Double Shift, but what if you want to find a specific string literal (we often want this when trying to find where log messages came from)? In that case, the other search command to know is "Cmd + Shift + F" which will find occurences of your search term not only in class and method names but truly everywhere in your codebase.

Bonus tip: I also find it useful sometimes to use the file mask option (you can see it in the top right hand corner of the image above) if i know maybe the thing I'm looking for is specifically in a html file vs a kotlin file or something like that to help narrow down the search results.

5. Select Opened File (Option + F1, then 1, but just clicking the button is easier)

Sometimes I find I want to know where the file I have open sits within the bigger folder structure. Often times it's because I want to take a look at another file that I know should be in the same folder but there are other uses for it as well. There's a very convenient button you can click (looks kind of like a target):

That will show you exactly where it is in the folder structure after you click on it

6. Jump to Line (Cmd + l, that is, lowercase L, not uppercase i)

This one's more useful for if you're doing code reviews and you want to load up what you're seeing on Github/Bitbucket on IntelliJ. You should be able to see the line number in the pull request for the code you want to check. First, in IntelliJ, jump to the class/file in question with Double Shift. Then hit "Cmd + l" (lowercase L), and enter the line number, hit OK and you're there!

Of course, this isn't that useful if your codebase is properly designed with small classes... But if you have to deal with a 5000+ line file, you'll be very glad you don't have to manually scroll everytime.

Visualization

1. Git Version Tracking Left Sidebar (Rollback Lines)

If you're modifying a long or complicated code snippet, it can be helpful to remind yourself what you changed (or even where you made a change) since the last commit.

IntelliJ is very helpful here if you're using some version control like git. It will color the left side of the code with either a green or blue bar, depending on whether you've added new lines or modified existing lines. If you just deleted lines, you'll see a small right-arrow-head pointing to where lines used to be before you deleted them.

These things are all clickable, and if you do click on it, you can see what used to be there before your changes. You also get buttons that can take you to the next change above/below, and more importantly, the option to just rollback these few lines at a very localized level. You can of course git rollback the entire file, but sometimes you want to be a bit more precise about what you reset.

2. Tabs (Split Right/Down)

It can often be very useful to be able to compare two different snippets of code side-by-side. IntelliJ (and probably every other code editor) has a very handy tool for letting you do this. Just right-click on the tab you want to shift over to the right or bottom side of the window and then click Split Right (or any of the other three options below it). Then you can easily compare similar classes against each other or even two different parts of the same file.

3. Visualization on Right Sidebar (Errors, warnings, changes)

If you look at the top right part of the code area. You can see some counts next to some symbols. If you mouse-over it, you can see they are counts of errors, warnings, and weak warnings. Clicking on it will open up a list of links that you can use to jump to where each potential issue sits. I actually never use that but it's handy to know.

What I do use is a bit harder to see, but if you look closely at the left side of the screen, you can see some bands of colors (red, grey, and yellow). These show basically the same thing but visually show you where they are in your file and you can easily scroll to it. If you hover your mouse over it, it will actually give you a little window into the code around the issues, with some information about what's wrong.

You might also notice that there's a tiny bit of blue just to the left of those bands of color. They also help show you along the entire file, where the changes you've made since the last commit are.

4. Project vs Structure toolbar

You will probably always have the "Project" toolbar open (usually on the left side). It's very useful for helping you navigate the folders of your project. If it's not open for some reason you can click the "Project" tab on the left to toggle it open and closed. Or just use Cmd + 1 if you can't find it.

What's less known and used is the "Structure" toolbar that usually opens below it. You can toggle it open and closed by clicking on the "Structure" tab or use Cmd + 7. It summarizes all of the methods/functions of the class that you have open. This is usually helpful if i'm looking at a large test class where each test is its own method. Can be a nice way to get a summary of all of the tests without having their details cluttering up the space.

Troubleshooting

1. Debugging with Breakpoints

Maybe you're one of those people that just put console.log/println statements everywhere to debug. If so, but you're using an IDE like IntelliJ, I highly recommend you try using debug mode with breakpoints instead!

Just click on the left side of the code at the line where you want to drop a breakpoint (you should see a red circle show up like below), and then click on the little green bug icon to start your application in debug mode (can also do something similar to run your tests in debug mode as well).

1.2 Resume Program, Step Over, Step Into, Step Out, and Dropping Frames

Once your program has stopped at a breakpoint. You can advance your program with the following five buttons: Resume Program, Step Over, Step Into, Force Step Into, and Step Out. These buttons are circled in red in the image below. Experiment with them to see what they do, they're pretty straightforward (except for "Force Step Into", I don't think I've ever used that one and I'm not sure what it does). The most useful one of them all for me is the "Resume Program" (the little green button on its own in the left side of the image). This one allows you to jump quickly from one breakpoint to the next, skipping all of the code in between that you don't care about.

Now you might have noticed that all of the buttons will only move your program forward. But what if you want to go back? Maybe you stepped a little bit too far and you don't want to have to restart everything just to go back to the previous breakpoint or previous line? You can't exactly step backwards, but what you can do is "drop frame" to jump back to just before the caller called the function you're currently stopped at. After that you can "step into" or "resume program" back to a breakpoint in your current function to go back to where you were previously.

Keep in mind, however, that any kind of variable mutation or DB changes won't be undone, so this is more useful if you're working with a more functional programming oriented codebase with mostly immutable code.

1.3 Variables List and Evaluate Expression

Once you're stopped at a breakpoint, you can see all kinds of useful information. Basically anything you might have wanted to console.log/println out, you can examine in the variables list

Even better, you can use Evaluate Expression to see how certain function calls might resolve given the current context of your application at the breakpoint.

2. Annotate With Git Blame

If you're wondering about the history of some piece of code (like why it was added or who added it), "git blame" can be very helpful. Just right click on the left side of your code (same area where you would left-click to put a breakpoint), and select "Annotate with git blame". Please note, even though it's called "Git Blame", don't use this tool to actually blame people, the main purpose of this is for investigation, and blaming people for poorly written code is never helpful. That said, it can be very helpful sometimes to ask the person behind that line of code for help in understanding it.

The left side will expand and show you the user and the date of change for every line. And if you left click on it, you can see the full set of changes from the commit for that line.

You can also right-click on it to "Show Diff" to see exactly what was changed and also "Copy Revision Number". This gives you the commit hash that you can then use to search on Bitbucket or Github for more information about that commit. If everything is linked properly, you could potentially jump from there to the related pull request, JIRA ticket, and maybe even project pages.

Writing Code (ie. asking IntelliJ to write it for you)

1. Tooltip suggestions (and Tab/Enter to autocomplete)

This feature is so useful and automatically available that I probably don't even need to talk about it. Most likely if you're already using IntelliJ you'll already be using this. As you type out your code, IntelliJ will automatically pop up a list of suggestions for what method name or object type you might be trying to write. Once you've typed out enough that your desired method call that it's at the top of the list, just hit Tab and IntelliJ will fill in the rest for you.

One thing you might not have realized is that if you're trying to change up something in the middle of a line of code, hitting tab would replace everything to the right with the suggestion. If you want to keep the text on the right (maybe they're some function arguments you want to keep), try hitting "Enter" instead of "Tab", and it will keep the text to the right instead.

2. Find + Replace (Cmd + f, Cmd + r)

Cmd + f is just a more localized version of the "find everywhere" feature that was described earlier. This one's limited to just the current file. Where this really shines is when you combine it with the "replace" feature. There's definitely a lot of different ways to use this but one of my favorites is if I want to turn a long new-line separated list of numbers into a single line comma-separated list (say for use in a db query), you can find the new-line character and replace it with a ",".

3. Refactor → Rename

If you want to change variable/class names, I highly recommend using Refactor-Rename instead of find+replace. IntelliJ will go out of it's way to rename all references to the variable when you do it this way, not just what's visible in the current file.

4. Lightbulb suggestions

Sometimes, you might notice IntelliJ highlighting some of your code in yellow.

If you hover your mouse over it, more often than not, you'll see a good explanation and suggestion for how your code could be improved.

If you click on the lightbulb, you don't even need to apply the fix yourself, just click on the suggestion you want to apply and IntelliJ will take care of it for you.

5. Block/Column Selection (Option + Left Click, hold and drag)

This one's more for editing/preparing data for tests than it is for writing code. But if you have columns of data you want to change, copy-paste, or remove, this tool can be very helpful. Just left click where you want to start, then hold down Option key, and left click, hold and drag down to where you want to go. Then you can edit, delete, or copy paste a column of text at a time.

6. Generate...

I was kind of joking earlier about IntelliJ writing our code for us but this feature is pretty much just IntelliJ doing just that.

There's a lot of boilerplate in java that's kind of a waste of time for us to write over and over again. So if you right-click and then select Generate... IntelliJ can just create a lot of this for you for free!

Just select what you want to generate, click through the various prompts that show up, and then you're done!

Of course, the fact that this kind of code needs to be written at all is kind of a waste. If you switch to something like Kotlin or Scala, you can get away without needing this generated code altogether!

Conclusion

I hope at least some of these were new and useful for you. IntelliJ is a great tool (and expensive if you're using it for commercial use) and it would be a waste to not make the most of it. Just keep in mind that a lot of coding interviews won't let you use these kinds of tools so if you're preparing for one (like say, if you're interested in joining us at Rakuten), you'll have to get used to using boring old notepad++ or something. I honestly think coding interviews should be done in a similar environment to what you would actually be working with (so that you'd have full access to your IDE and the internet) but for some reason that's not how things are at the moment.

Anyways, hope this helped and happy coding!