My favorite keyboard shortcuts for basic code editing

keyboard shortcuts only

Taking your hand off the keyboard and putting it on a mouse is an expensive operation. It costs a lot of time and energy and although it sounds like a joke I am actually serious.

Compared to having your hands lying still and just tapping fingers, any whole-arm movement suddenly feels like a helluva work. Maybe I’m that lazy, but actually just moving my right hand to reach the arrow keys bothers me somewhat.

Also, consider how much slower it is to grab a mouse and point the cursor to a tight space right between two characters where you want to do some editing. Or tap the arrows like a hundred times to move the caret to a place few words away.

As programmers, we do those things enough times a day for all those inefficiencies to be a big deal.

One map to rule them all

Both IDEs and glorified text editors help us avoid paying the hefty price of excessive arm movement with keyboard shortcuts. The problem is that every tool comes with its own keymap, and if you’re switching between editors often, it becomes a problem.

So I’ve finally decided to pick my favorite shortcuts and apply them to all the tools I use. I am a long-time user of IntelliJ and now Rider, so my choice is based on their defaults. However, since I also regularly write in VS Code, I will focus on basic code editing commands that don’t require fully fledged IDE to be available.

I really suggest giving them all a try if you haven’t already.

Note on learning those shortcuts

Learning keyboard shortcuts in a new tool takes a while. There are always some commands that stick well, while some others stay in oblivion. Here’s what I think can help to master them.

Firstly, I like to think of commands in categories of function. Usually, those similar functions will be mapped somewhat symmetrically – just adding a modifier key brings us from one to another. It is best to learn those categories as a whole rather than shortcut by shortcut.

Pick one category. Try out each of the commands. Then tell yourself that you will be deliberately using them for the next few minutes of coding. Watch yourself and think ahead what are you about to do. You will be going against your habits now and it’s always a bit difficult. Do it a couple of times. Repeat the exercise the next day. Eventually, it will start happening automatically and you will no longer have to think about it. Your goal is to have your editing intentions translated directly to the new behavior.

My favorite command groups

So here are those basic code editing commands I like the most.

Inline caret movements, selects and deletes

Those are for moving caret left and right within the current line. It’s all very similar whether you intend just to move the caret, move and select everything from here to there, or to immediately follow it all up with hitting backspace or delete.

StepCaret+ Select+ Delete
characterleft/rightshift + left/rightbksp/del
wordctrl + left/rightctrl + shift + left/rightctrl + bksp/del
line beginning/endhome/endshift + home/endalt+shift+bksp/del

Line operations

Moving on to manipulating whole lines of code. I find myself editing something in the middle of a line and then immediately wanting to either delete it or start a new one pretty much all the time. It is very convenient to be able to do that with one keystroke regardless of where the caret is.

Start new lineshift + enter
Start new line abovectrl + alt + enter
Move line up / downalt + shift + up/down
Duplicate line at caretctrl + d
Delete line at caretctrl + y
Select line at caretctrl + shift + y

Go to commands

Another set of commands I find myself using all the time. Very handy when navigating large solutions with many directories.

Go to classctrl + n
Go to filectrl + shift + n
Go to symbolctrl + alt + shift + n
Go to declarationctrl + b
Go to type declarationctrl + shift + b
Go to testctrl + shift + t

Meta navigation functions

Among all the navigation functions the ones I hold the dearest are those allowing me to go where I was before or where I did my last edits. Thanks to those, I can quickly get on with whatever I was doing before going down the rabbit hole, which often results with a lot of opened files.

Undo / Redo navigationctrl + alt + left/right
Go to previous / next occurrencectrl + alt + up / down
Go to last edit locationctrl + shift + bksp

Missed anything?

Got any other code editing ninja moves I am apparently unaware of? Don’t hesitate pointing out my ignorance in the comment section. I will really appreciate it.

Leave a Reply

Your email address will not be published. Required fields are marked *