Comfy keyboard shortcuts for RStudio

Aimed at beginner and intermediate users
R
RStudio
Author
Published

August 4, 2023

Intro

Found, by Helvetica Blanc

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?

Behold!

You can even work your way through a script by hitting Command + Enter on repeat. Donā€™t trust me?

Try this for size.

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ā€.

Click into the empty middle column for Switch Focus Between Source/Console. Press whatever shortcut keys you want to use, then hit enter. I used Control + 1, but this was already assigned to Move Focus to Source so I had to delete it there too.

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

In this example I add carets to multiple contiguous lines of text.

: 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 this example I delete several lines across a few blocks of text.
Note

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.

Using just ", (, and [ to enclose some text.