Well it’s Halloween again. A time to ponder about what scares us. I’ll go first. I get scared when I have an idea while coding, and I know that I won’t be able to finish actually typing the code out before I forget what I’m doing. This might sound dumb, but when you’re learning a new programming language, battling with syntax, and constantly referencing the help menu and google, it can take awhile to actually type out your code. If you share a similar fear, and don’t currently use keyboard short cuts in RStudio, I have some good tips for coding faster, but if you’re an experienced RStudio shortcutter, I’m sure you’ve already alt + tab’ed out of this post. Let’s start with the basics:
One of the best ways to speed up your coding is to stop using your mousepad and keep your fingers on your keyboard. For instance, instead of clicking on the little floppy disc every time you want to save your script, just hold control (command for macs) and type “s”. This will save you lots of time, and it is easier to develop good saving habits using the short cut. Heres another simple one: say you are coding along and then all of a sudden, you want to query google about formatting ggplots. Instead of moving the mouse to the task bar and switching to your internet browser, just hold alt (again command for macs) and hit “tab.” Boom! You just switched windows without sliding a finder.
Okay those were simple ones that most of you probably already use. Lets get a little more R-specific. How about when you finish writing a big long comment and you realize you forgot to put the “#” at the beginning of the line, meaning your “comment” is now a bunch of unusable code. Well you could either slide your cursor to the beginning of the line, click and then type #, but that takes waaaay too long. Instead, leave your cursor where it is and hold control+shift+c (substitute command for control for macs). Kapow!!! The whole line is instantly transformed into a comment! Scenario 2: you just realized a whole block of code is useless and you need to start fresh, but you don’t want to delete what you have. I’ll give you permission to use your mouse here but you could hold shift and use the directionals as well. Highlight all the bad code and hold control+shift+c. Now it’s all commented! Want a line or two back? Just highlight the lines you want to uncomment and hold control+shift+c again. SNAP! It’s code again.
Want to copy a line of code without using copy/paste? Use shift+alt+up/down (command+option+up/down for macs) to replicate a favorite line of code as many times as you want! Stick a variable in there and you have a poor man’s for-loop (just kidding,that wouldn’t save any time).
Sick of clicking on that “knit PDF” button over and over? control+shift+K.
Want to delete a whole line of code? control+D
The list of things you can do with keyboard short cuts is exhaustive. They have short cuts for code folding and debugging too if you’re into that. Check it out here or if you don’t want to leave R to reference the short cut list, just hold alt+shift+K (option+shift+K for macs) and it will pop right up. Thats right, there is a short cut for finding short cuts.
Well I hope you got something out of this post. I figured it would be useful information to have before we all sit down to hammer out our final projects. Happy Halloween. Happy coding.