Keyboard Shortcuts for Programmers
After watching my bootcamp instructor highlight 10 words in as many sentences, all in about half a second, I knew I needed to step up my shortcut game. A quick Google search for MacBook keyboard shortcuts left me feeling a bit overwhelmed, so I decided to do a little research and compile a list of those I’ve found to be most useful to me thus far. It covers some basics, which you may already be familiar with, as well as some text editor shortcuts. I’ll talk a bit more in depth about some of my favorites/more recent discoveries below, and have also included a table with a full list at the end.
So how exactly did my instructor highlight all those words? Turns out it was Command-D. This is a VS Code shortcut, and is a nice alternative to highlighting all occurrences, as it selects only the next occurrence matching the highlighted text. You can select multiple occurrences, however, by holding down Command, and pressing D as many times as you want occurrences. I’ve found this to be particularly useful when writing data seeds. I often copy the code to create a new instance, then re-paste it a few times and edit each with unique argument values. On more than one occasion I’ve left off a comma or colon, and with Command D I can highlight the word missing punctuation, hit the right arrow key (or left if it’s missing before), and add my punctuation to all occurrences at once
Next let’s talk about Command-D’s cousin, Command-Shift-L. Say you created a variable or method and decide you want to rename it, but oh no! You’ve already called on it several times throughout your code. Enter Command-Shift-L. Unlike Command-D, Command-Shift-L in VS Code highlights ALL occurrences matching the selected text. Instead of sitting there hitting D to find all your occurrences, you can use Command-Shift-L instead to highlight, and then change them all at once.
We’ve all been there, you’re writing some code and decide to scrap a line. You’ve probably been scrolling to the end and holding delete until it’s finally gone. Well, no longer. Command-Shift-K in VS Code has you covered. It’s simple; it deletes the entire line that your cursor is currently on
⌘-] & ⌘-[
We all know how important it is to have pretty, clean, code. I don’t know about you, but often times when I’m working through a method, things get a little messy, and formatting might not be my number one priority at that moment. Command-] allows you to tab a line from within, and saves you from having to go back to the beginning to use Tab. Conversely, Command-[ allows you to unindent from within a line, and makes it easier to keep your code neat when working through problems.
This one took me a little more digging to find than I’d like to admit (my googling skills have since improved). Command-/ in VS Code comments/uncomments out any highlighted lines of code. It still works for single lines, but it’s most useful for commenting out big chunks. Be forewarned though, if you have a commented out line of code within the larger block you want to comment out, it will double comment out the single, already commented line. You’ll need to go back in after uncommenting the large block and further uncomment the single line.
For whatever reason, I always have a million applications open on my computer. A MacBook screen is only so big, and it’s easy to lose what I’m looking for. This is a more common command, but one I only recently discovered. In that time though, I’ve been making good use of it, and all my applications live to stay open another day! Command-Tab pulls up all of your open applications and allows you to easily switch between them using either Tab, or the side arrows. I personally prefer to Command-Tab, keep holding down command, and scroll through with the left and right arrows as it provides more flexibility.
Can’t find your terminal? Command-Space! At least this is what I find it most useful for. What it does is pull up Spotlight Search on your Mac. Before adding it to my dock, I was always losing track of my terminal, so I mainly used this shortcut to search for and open it, but you can use it to search for any and all content on your computer
As software engineers, we do a lot of googling. Chrome has a great feature to make switching between tabs easier, Command[1–9] . So long as you know which tab you’re looking to open, you can hold Command and then the tab’s number. One thing to note is that this only works for single digit tabs, and 9 will return the tab furthest to the right, regardless of its number. Alternatively, you can use Control-Tab to go through all of your tabs until you get to the one you’re looking for
These are just some of the shortcuts that I’ve found have made the biggest difference in my efficiency and cleanliness as a programmer. Hopefully this was helpful and you picked up a new shortcut or two to add to your arsenal, either from those described above, or from those in the table below!