Intro
I know quite a few keyboard shortcuts in RStudio these days.
That said, such knowledge has been hard won. My background is not in programming, so a lot of the time I wasnāt aware that certain shortcuts were possible. Or if I did, I may not know how to describe them/their names.
Much of my learning has been done by bumbling around, giving it a go, and pairing with colleagues. Thatās all good, but it is nice to have a practical list for reference.
So, with help from some good peeps over at mastodon, Iām happy to share just such a reference list. The aim is to help get you feeling confident, comfy, and fancy-free when coding in RStudio. Itās nice to feel at home in your IDE of choice āŗļø
Iām a Mac user currently running RStudio 2023.06.1. Please let me know if thereās anything wrong with the Windows/Linux defaults listed.
Keyboard shortcut quick reference
: Option
+ Shift
+ K
/ : Alt
+ Shift
+ K
Brings up the keyboard shortcut quick ref.
However, I can never remember the layout of the panel and get mentally overloaded seeing so many symbols and columns on screen at once. I prefer the web version instead (though it is a bit out of date).
Restart R session
: Command
+ Shift
+ 0
/ : Ctrl
+ Shift
+ F10
Generally, itās a good habit to restart the R session regularly. If your global environment is a graveyard of variables and functions - and you canāt tell which are living or dead - this is what you need.
Executing code
: Command
+ Enter
/ : Ctrl
+ Enter
Executes the line, or lines, your cursor is on.
Do you highlight an entire series of piped functions with your mouse and then click the Run
button?
What if I told you that instead, you just put your cursor wherever the hell you like in a pipe and press Command
+ Enter
? And it just runs the whole thing?
You can even work your way through a script by hitting Command
+ Enter
on repeat. Donāt trust me?
Thatās right: Even when the focus is in the console (which you can tell because the cursor is blinking there) you can continue hitting Command
+ Enter
to run subsequent lines of your R script.
Switch focus between Source/Console
/ / : Your
+ choice!
Toggle the keyboard focus between your script and the console.
This is an unbound keyboard shortcut by default. While there are shortcuts to focus individually on the source and console panels, I like this one because it acts like a toggle. Easier to remember than two separate shortcuts. Also my brain smallā¦ like cute cat :)
Go to Tools
, Modify Keyboard Shortcuts...
and search for āfocusā.
Indenting code
: Command
+ I
/ : Ctrl
+ I
Standardises indentation for highlighted code.
Youāre typing, copying from the web, moving stuff about. I know it gets messy. Forget tab/space/backspace tedium and use this shortcut.
Commenting crap
: Command
+ Shift
+ C
/ : Ctrl
+ Shift
+ C
Comment/uncomment the line your cursor is on, or highlighted lines. Trust me that this one is worth memorising. Youāre going to be using it a lot.
Finding crap
: Command
+ F
/ : Ctrl
+ F
Brings up the find/replace menu for the script currently open. The In selection
check box is very useful.
: Command
+ Shift
+ F
/ : Ctrl
+ Shift
+ F
Brings up the Find in Files
dialogue box. I use this when itās been a while since I worked on a project. For example, I might want to know which files a specific function gets used in.
Inserting operators
: Option
+ -
/ : Alt
+ -
Drops in the assignment operator <-
: Command
+ Shift
+ M
/ : Ctrl
+ Shift
+ M
Drops in the pipe operator.
If youāve had RStudio installed for a while the default might be the magrittr
pipe %>%
. These days, the base R pipe |>
has got you covered for 99% of use cases, and doesnāt add yet another dependency or library call to your projects, so it is my preferred pipe.
If you want to make it the default, go to Tools
, Global Options
, and check the box as in the image below.
Using multiple carets/cursors
These are for when you want to do stuff like āfind and replaceā, but the text youāre finding/adding/removing is heterogeneous in some way. You might be tidying up some lines of text after pasting them from a web page, for example.
: Control
+ Option
+ Up/Down
/ : Ctrl
+ Alt
+ Up/Down
Adds carets above/below your current position.
: Option
+ Click and drag
/ : Alt
+ Click and drag
Highlight text or place carets in contiguous chunks.
- Click/move your cursor into the first place you want a caret
- Hold down
Option
- Click and drag the cursor
: Control
+ Option
+ Click
/ : Control
+ Alt
+ Click
This is for when things have started to get a little bit messier and you canāt easily highlight text in contiguous chunks.
- Click/move your cursor into the first place you want a caret
- Then hold down
Control
+Option
- Each mouse click will place another caret wherever your mouse is hovering
In my experience the terms ācaretā and ācursorā are are synonymous in programming IDEs like RStudio. However, some IDEs will use ācaretā and others ācursorā. One to be aware of.
Moving the cursor
: Command
+ Up/Down/Left/Right
/ : Ctrl
+ Up/Down/Left/Right
The up/down keys get you to the top/bottom of a document.
The left/right keys get you to the start/end of the current line.
: Option
+ Left/Right
/ : Ctrl
+ Left/Right
Skip over whole words. Whee!
Highlighting and deleting text
: Option
+ Shift
+ Left/Right
/ : Ctrl
+ Shift
+ Left/Right
Highlights whole words. Oooh š®
: Option
+ Backspace
/ : Ctrl
+ Backspace
Deletes the word preceding the cursor position. Cya š
Summon the command palette š¹
: Control
+ Shift
+ P
/ : Ctrl
+ Shift
+ P
Bringing up the command palette lets you search for commands (and settings) by name. Here I bring up the palette because I can never remember the shortcut to turn some text into a function.
Enclosing
Highlight some text and enclose it with e.g. parentheses ()
or quotes ""
.
If you highlight some text, you only need to give the opening character of the enclosing pair. RStudio will add the closing character.