New IntelliJ Tricks I Learnt Part 2
Join the DZone community and get the full member experience.Join For Free
apparently, i’m all about continuous learning - after i wrote the last post about intellij tips , i carried on through my trawl of the documentation and, of course, discovered a load more things i didn’t know. and this is all just in the quick start section!
so here’s the next batch (although once again i’m not sure how many are only applicable to intellij 14). all shortcuts are for windows unless otherwise stated, and mac users should be able to replace the ctrl key with cmd in all these cases.
navigate between methods
now i knew you could use the structure thingie (ctrl+f12) to let you navigate to methods in your class (although i never actually use it, it somehow never sticks in my head). what i did not know is that you can jump between methods using alt+up/down arrows. this strikes me as being super useful, especially to browse tests.
refactoring string fragments
i’ve wanted this feature so many times, but could never find it or get it to work - did you know you can refactor out parts of a hardcoded string into variable/field/constant? select the section of the string you want to extract, and use the normal “extract…” shortcuts - e.g. ctrl+alt+v to extract it into a variable.
i’m sure i’ve tried this before and it didn’t work. but i’ve tested it and it does!
there doesn’t seem to be a keyboard shortcut mapped by default, but using “refactor -> invert boolean…” (or using
our newly discovered
“navigate to action” shortcut of shift+ctrl+a and typing “invert”) you can tell intellij you want a boolean field to mean the
thing, and it will
flip the meaning everywhere
. for example, you might have an
flag, and you now want it to mean
- intellij will make sure the
magic refactor suggestions
you don’t always know exactly what refactoring operation you want to perform, and intellij is ready to help - it can suggest refactorings available in the current context using the catchy magic incantation ctrl+shift+alt+t. seems useful, but i’m pretty sure i’m not going to remember that one.
highlight implemented/overridden methods
i love everything about intellij’s ability to find usages of things - finding where methods are called, finding implementations of a method, highlighting usages of a field, method or variable… what i didn’t know, is that you can highlight the methods that implement or override a specific interface or superclass . for some people, this might not add much, as your little “i override something” icon
combined with the
annotation, might tell you all you need to know. but that only really works if you’re implementing a a single interface. if you’ve correctly separated your concerns, you might implement several interfaces, and want to know which are the methods from a particular one. that’s where shift+ctrl+f7 on the “extends” keyword comes in handy.
run individual inspection by name
i use inspections quite a lot. they’re especially useful if you have a codebase that doesn’t quite conform to your ideal standards, so you can’t yet turn on automated checks like checkstyle to break the build when the code doesn’t conform - there’s an initial step where first you need to clean up the code. inspections are great, because you can have intellij give you little yellow marks to show where things are suboptimal, and aim to check in only files without little yellow marks.
what i didn’t know, is that you can run a single inspection on files in any scope. so if, for example, you wanted to make sure that the code you’ve touched in this change has no unused bits, like unused parameters or unused/untested methods, you could press shift+alt+ctrl+i and type “unused”. select the “unused declaration” inspection and choose to run this on all uncommitted files. this will tell you where you can clean up your code, and every commit should make your codebase that little bit better.
saving power with highlighting
i did not know you could easily alter the active errors/warnings , and use that to save power. quite interesting.
auto indent your lines
i almost obsessively reformat my code as i go, pressing ctrl+alt+l almost like a muscle twitch. what i’ve just discovered, though, is that there’s a shortcut which will just fix the line indentation , which is probably 80% of what i need anyway - ctrl+alt+i.
fix evil string concatenation
i can’t believe i didn’t notice this one earlier - do you know how many times i’ve gone around and manually changed:
system.out.println("this thing " + something + " is " + somethingelse);
system.out.printf("this thing %s is %s%n", something, somethingelse);
do you??? anyway you don’t need to - alt+enter inside the params will suggest the replacement and do it for you . nice.
vcs quick access
one of the areas where i definitely don’t use intellij to the best of its abilities is the git integration. i’ve been using sourcetree combined with intellij to do most of my vcs management (i’m so sorry, i’m just not a command line person, i need to actually see it). with the new laptop, i haven’t installed sourcetree yet and i’m going to try and do everything via the ide. previously i used the right-click menu mostly for managing vcs operations, occasionally the vcs menu, and did most stuff in the “changes” window. but now i’ve discovered alt+“back quote” (ctrl+v on the mac) to bring up the menu of git-shaped operations for this context . to stop you having to hunt all over the keyboard to find what on earth a “back quote” is, it’s `, which on both my uk and us (windows) keyboards is in the top left corner of your keyboard (on the mac, it’s on the bottom left next to the shift, but you shouldn’t need it since your shortcut is different anyway).
i’ve just discovered that you can commit a change which smushes up into your last commit and fixes it. i’m assuming it commits the new changes and (potentially) an updated commit message into the last change. i really wish i’d known about this earlier, on a number of occasions in the last few months i’ve done this either manually or by doing a rather dangerous soft reset. i will definitely be using this in the future.
the cursor as a breakpoint
did you know that you can use the cursor as a temporary breakpoint ? i did not. well, now you know.
i hope by now none of us developers are doing that icky thing of having “copy of…” and “backup of…” filenames and directories to keep track of our versions. we’re all using a vcs of course. but if you’re being strict about making sure that every commit leads to a working build (we all do that, right…?) then sometimes even distributed file systems like git, with its local branches, are too heavyweight for experiments and debugging.
intellij has local history, which i personally think you should never rely on, but has occasionally saved my bacon in times of strife. it also does this really nice thing (automatically) where it labels versions where the tests passed, so you’ve got some level of confidence that at that point in time, at that revision, things weren’t hideously broken. at the very least, they must have compiled. what i did not know is that you can add your own labels , so you could label some local, non-committed version of the file as “feature x works but y doesn’t” (or probably something much shorter).
i think it looks useful. as i say though, not a replacement for vcs, but a suppliment.
so, that’s the end of week one at jetbrains, and already i’ve learnt a whole heap of things. i’m sure there’s more to come, so stay tuned!
Published at DZone with permission of Trisha Gee, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.