Skip to content

Instantly share code, notes, and snippets.

@kepler471
Last active July 22, 2025 15:23
Show Gist options
  • Save kepler471/062f35434874af5b378891de51747e5c to your computer and use it in GitHub Desktop.
Save kepler471/062f35434874af5b378891de51747e5c to your computer and use it in GitHub Desktop.
# Keyboard Shortcuts and RStudio Guidance for Windows
> **Note**: This guide focuses on Windows keyboard shortcuts for RStudio.
We felt it might be useful to put together a collection of the most useful keyboard shortcuts and RStudio tips, as many guides and courses on the R language do not include much information on how to work with RStudio. Once you get used to using keyboard shortcuts, you will notice an improvement in productivity, and it makes for a smoother user experience.
This is not the complete set of keyboard shortcuts, it is more of a curated selection. For the complete keyboard shortcuts reference, see the [official documentation](https://docs.posit.co/ide/user/ide/reference/shortcuts.html). You can also press `Alt + Shift + K` in RStudio to show a quick reference.
Each section below contains an *interactive* code chunk. Some chunks are deliberately incomplete or messy so that you can practise the shortcut.
---
## Moving around RStudio
RStudio's interface consists of multiple panes that you can navigate between quickly using keyboard shortcuts. Learning to move between panes and zoom them helps you focus on specific tasks.
> 📚 **Learn more**: [Pane Layout](https://docs.posit.co/ide/user/ide/guide/ui/ui-panes.html) | [Accessibility Features](https://docs.posit.co/ide/user/ide/guide/accessibility/accessibility.html)
Practise focusing panes and zooming:
1. Press `Ctrl+1` to focus the **Source** pane.
2. Press `Ctrl+2` to jump to the **Console**.
3. Hit `Ctrl+Shift+1` to *zoom* the Source, then the same shortcut again to restore.
4. Cycle panes with `F6` / `Shift+F6`.
Use the View top bar menu to manage the panes. "View > Pane Layout" can be used to manage the positions of your panes.
---
## Running and executing R code
Ctrl+Enter runs a section of code. This can be a line or a section, or if some code is highlighted, then it will only run the highlighted region.
> 📚 **Learn more**: [Code Execution](https://docs.posit.co/ide/user/ide/guide/code/execution.html) | [R Console](https://docs.posit.co/ide/user/ide/guide/code/console.html)
This shortcut has some key functionality that often is overlooked.
- **You do not need to highlight all the code that you want to run**.
- You can place the cursor anywhere in a section of code
- This means anywhere in a pipe, anywhere in a line/brackets
- **Works differently for functions**
- Place cursor on line with opening or closing function brackets (`{`, or `}`)
- Using Ctrl + Enter from within the function will run only that section of code
- **Rmarkdown/Quarto**
- Using these commands on code in .R files "echoes" the code in the terminal, and then produces the results in the terminal
- Using these commands on code in .Rmarkdown or .qmd files will echo the code in the terminal, but the results will be attached to that code chunk in the document
- Use **Ctrl + Shift + Enter** to run an entire code chunk in these documents. Without Shift, the code execution will behave as normal, only running the selected section
### Quick‑reference
| Action | Keys |
| -------------------- | ------------------------------------ |
| Run line / selection | `Ctrl + Enter` |
| Run code chunk in Rmd/qmd | `Ctrl + Shift + Enter` |
| Run line / selection without moving cursor to next section | `Alt + Enter` |
| Run to cursor | `Ctrl + Alt + B` |
| Run from cursor | `Ctrl + Alt + E` |
| Run entire script | `Ctrl + Alt + R` |
| Source with echo | `Ctrl + Shift + Enter` |
| Knit / Render | `Ctrl + Shift + K` |
### Try it
```{r}
# 1️⃣ Place the cursor on each line and hit *Run* (Ctrl+Enter)
a <- 1:5
b <- a^2
# 2️⃣ Put the cursor *above* the plot() call and press Run‑to‑cursor
plot(a, b)
# 3️⃣ Now move the cursor to the plot() line and press Run‑from‑cursor
summary(lm(b ~ a))
```
```{r}
# Highlight the whole pipe or place the cursor on any line
# then press *Run* (Ctrl+Enter) repeatedly to step
# through, or highlight the entire block and run it at once.
library(dplyr)
iris |>
filter(Species == "setosa") |>
mutate(Sepal.Ratio = Sepal.Length / Sepal.Width) |>
arrange(desc(Sepal.Ratio)) |>
# TODO: Add another dplyr verb (e.g., slice_head(n = 10))
head()
```
---
## Editing code
Here are shortcuts for some of the most common actions you will perform when writing code.
> 📚 **Learn more**: [Text Editor](https://docs.posit.co/ide/user/ide/guide/productivity/text-editor.html) | [Code Diagnostics](https://docs.posit.co/ide/user/ide/guide/code/diagnostics.html)
### Quick‑reference
| Action | Keys |
| ---------------------- | -------------------------- |
| Comment / uncomment | `Ctrl + Shift + C` |
| Duplicate line | `Ctrl + D`, `Alt + Shift + Up/Down` |
| Insert pipe | `Ctrl + Shift + M` |
| Insert assignment `<-` | `Alt + -` |
| Delete line | `Ctrl + Shift + Backspace` |
| Delete line to right of cursor | `Ctrl + K` |
| Find | `Ctrl + F` |
| Find and replace | `Ctrl + F` (then toggle Replace) |
| Expand selection | `Ctrl + Shift + Up` |
| Shrink selection | `Ctrl + Shift + Down` |
### Fix this snippet
```{r}
# TODO: Uncomment the line below
# TODO: Insert the assignment operator in the correct position
my_favourite_numbers c(2, 1467, 99998)
```
```{r}
library(dplyr)
# TODO: Correct this piped section by adding pipes with the Insert Pipe shortcut
iris
filter(Species == "setosa") |>
mutate(Sepal.Ratio = Sepal.Length / Sepal.Width)
arrange(desc(Sepal.Ratio))
head()
```
### Find and replace
```{r}
# Practice find and replace within this file:
# - Press Ctrl+F to open Find
# - Search for "temp" in the code below
# - Click the Replace toggle in the Find bar
# - Replace all "temp" with "temperature"
temp_data <- c(20, 22, 21, 23, 19)
mean_temp <- mean(temp_data)
print(paste("Average temp:", mean_temp))
# Find options:
# - Match case
# - Whole word
# - Regular expressions
# - In selection only
```
---
## Formatting code
Formatting code makes code consistent, cleaner, easier to read, and easier to maintain.
There are multiple ways to apply formatting, and there are multiple "styles" that the code can be formatted to.
The recommended style is tidyverse style. To make sure you are using tidyverse style, install styler (`install.packages("styler")`). Restart RStudio, and you should see that a Styler section appears in the "Addins" drop down menu in the top bar. These can be used to format your code, or you can use the RStudio keyboard shortcut, Ctrl + Shift + A.
Go into "Tools > Global Options" in the menu bar at the top of RStudio. Click into the Code section on the left panel, then the Formatting tab, and choose styler as your code formatter in the drop down menu. Now when you use Ctrl + Shift + A it will do the formatting with styler rather than the RStudio default.
You may not like the changes that the formatter applies to your code, sometimes it may make the code less clear, and so feel free to take the formatter as a suggestion.
When using Ctrl + Shift + A, select the region you would like to format. Or if using styler, use the Addins drop down to choose whether to style the selection, the file, or a whole package.
> 📚 **Learn more**: [RStudio Add-ins](https://docs.posit.co/ide/user/ide/guide/productivity/add-ins.html) | [Text Editor](https://docs.posit.co/ide/user/ide/guide/productivity/text-editor.html)
### Quick‑reference
| Action | Keys |
| ------------------------- | -------------------------- |
| Re‑indent / tidy | `Ctrl + Shift + A` |
| Re-indent only | `Ctrl + I` |
| Reflow comment paragraphs | `Ctrl + Shift + /` |
### Ugly blocks — make them beautiful
```{r}
## This code is intentionally ugly; highlight it and run the formatter.
result<-lapply(1:3,function(i){sqrt(i)+1})
print(result)
```
```{r}
# The following helper is a mess. Do **not** run it yet.
bad_code=function( x){y<-x+1;return( y) }
```
After formatting, the function should look like:
```{r eval = FALSE}
clean_code <- function(x) {
y <- x + 1
y
}
```
```{r}
# Use the reflow action to spread this comment to multiple lines. It is better practice to limit the line length in your files. Although you can set up the editor to visually "reflow" the comment, some users may not have this setting applied, and so they will have to scroll sideways to read the entire comment line.
abc <- 123
```
---
## Navigating a document
Efficiently move through your code and documents using these navigation shortcuts. They help you jump between sections, navigate by word, and quickly access different parts of your file.
> 📚 **Learn more**: [Code Navigation](https://docs.posit.co/ide/user/ide/guide/code/code-navigation.html) | [Code Sections](https://docs.posit.co/ide/user/ide/guide/code/code-sections.html)
### Quick‑reference
| Action | Keys |
| ------------------------- | ------------------------------ |
| Go to start / end of file | `Ctrl + Home/End` |
| Navigate between sections | `Ctrl + ↑/↓` |
| Show document outline | `Ctrl + Shift + O` |
| Jump To menu | `Shift + Alt + J` |
| Insert section header | `Ctrl + Shift + R` |
| Move by word | `Ctrl + ←/→` |
| Go to line start / end | `Home/End` |
| Select while moving | `Shift + [movement key]` |
### Navigate this document
```{r}
# 1️⃣ Press Ctrl+Shift+O to see the document outline
# 2️⃣ Try Shift+Alt+J to open the Jump To menu
# 3️⃣ Use Ctrl+↑/↓ to jump between sections
# 4️⃣ Try Ctrl+Home to jump to the very top
# Place cursor in middle of this comment and:
# - Use Ctrl+← to move word by word
# - Use Home to jump to line start
# - Hold Shift while moving to select text
```
### Create a new section
Note that this only works in R files, not Rmarkdown or Quarto.
These sections are viewable in the Document Outline.
```{r}
# Press Ctrl+Shift+R to insert a section divider
# Type a name like "My Analysis" and see the formatted header
# Press Ctrl+Shift+O to view the new section in the Document Outline
```
---
## Multi cursors
Multi cursors are powerful for editing multiple locations simultaneously.
Combine them with Shift and other navigation keybindings to easily select and edit text at each cursor position.
> 📚 **Learn more**: [Text Editor - Multiple Cursors](https://docs.posit.co/ide/user/ide/guide/productivity/text-editor.html)
### Quick‑reference
| Action | Keys |
| ------------------------ | ---------------------------- |
| Add cursor above / below | `Ctrl + Alt + ↑/↓` |
| Add cursor (mouse) | `Ctrl + Alt + Click` |
| Escape multi cursors | `Esc` |
### Practise: replace the placeholders
```{r}
# TODO: Replace every occurrence of VAR with a better name in one go.
# Hint: sometimes find & replace might be easier
VAR <- rnorm(100)
hist(VAR)
mean(VAR)
sd(VAR)
```
```{r}
library(dplyr)
library(janitor)
# TODO: Remove all the explicit namespace declarations (`dplyr::`, `janitor::`)
iris |>
dplyr::filter(Species == "setosa") |>
dplyr::mutate(Sepal.Ratio = Sepal.Length / Sepal.Width) |>
dplyr::arrange(desc(Sepal.Ratio)) |>
janitor::clean_names() |>
dplyr::as_tibble()
```
---
## Viewing data
RStudio provides multiple ways to explore your data interactively. The data viewer is particularly helpful for understanding data structure and content, and offers simple ways to sort and filter data. Any data saved as a variable can be viewed in the data viewer with the R function `View()`, and this can even be attached at the end of a pipe to get a view of some data that you have not yet assigned a name.
Stored objects, anything assigned to a name, are saved within your "environment", which can be seen in the Environment pane. You can see there is a drop down to change environments, and here you can explore the things exported by the libraries you have imported, such as functions imported by `library(dplyr)` - but the Environment pane is a useful area that helps us track the objects we have created. Feel free to switch between list or grid view. Both views offer a small spreadsheet-like icon, clicking this is equivalent to using the `View()` function. The Environment pane also shows you object sizes, object types, and other metadata. The List view groups items by type, and the Grid view has a Type column so you may sort them by that column to get a similar grouping to the List view.
> 📚 **Learn more**: [Data Viewer](https://docs.posit.co/ide/user/ide/guide/data/data-viewer.html) | [Local Data](https://docs.posit.co/ide/user/ide/guide/data/data-local.html)
### Quick‑reference
| Action | Keys |
| ------------------------- | ----------------------------- |
| View data in new tab | `View()` function |
| View object (click) | `Ctrl + Click` |
| Filter/sort in viewer | Click column headers |
| Search in data viewer | Use search box in viewer |
### Explore your data
```{r}
# Create some sample data
my_data <- data.frame(
name = c("Alice", "Bob", "Charlie", "Diana"),
score = c(95, 87, 92, 88),
grade = c("A", "B", "A", "B")
)
# Run View(my_data) to open in data viewer
# Or Ctrl+Click on 'my_data' in the Environment pane
# Try clicking column headers to sort
# Use the search box to filter rows
# View(my_data) # Uncomment and run
# For larger datasets
# View(mtcars)
# View(iris)
```
### Data viewer features
```{r}
# The data viewer supports:
# - Sorting by clicking column headers
# - Filtering with the search box
# - Viewing large datasets efficiently
# - Copying selected data
# Try with built-in datasets:
# Ctrl+Click on these in the Environment pane after running
data(mtcars)
data(iris)
```
---
## Finding things fast
For working across a larger code base with many functions and/or multiple files, there are shortcuts that can help us navigate faster.
`Ctrl + .` is particularly useful, as you can quickly search across files and functions within the directory. There are also options to search through all the open tab/windows, as your workspace can often get cluttered, or doing a text search across the whole working directory.
> 📚 **Learn more**: [Code Navigation](https://docs.posit.co/ide/user/ide/guide/code/code-navigation.html) | [Command Palette](https://docs.posit.co/ide/user/ide/guide/ui/command-palette.html)
### Quick‑reference
| Action | Keys |
| --------------------- | -------------------------- |
| Command Palette | `Ctrl + Shift + P` |
| Go to file / function | `Ctrl + .` |
| Go to open tab | `Ctrl + Shift + .` |
| Find in files | `Ctrl + Shift + F` |
### Where's *mystery\_function()*?
```{r}
# TODO: Define mystery_function() somewhere in this project, maybe in another file.
# Use *Go to file/function* or *Find in files* to jump to its location.
```
### Using Find in Files
Find in Files (`Ctrl + Shift + F`) searches across your entire project:
1. **Search scope**: Choose to search in project, directory, or custom folders
2. **File patterns**: Filter by file type (e.g., `*.R`, `*.Rmd`)
3. **Regular expressions**: Use regex for complex pattern matching
4. **Case sensitivity**: Toggle case-sensitive search
5. **Replace across files**: Find and replace text in multiple files at once
---
## Files & Projects
Manage your files and projects efficiently with these shortcuts for creating new scripts and saving your work.
> 📚 **Learn more**: [Managing Files](https://docs.posit.co/ide/user/ide/guide/ui/files.html)
* Press `Ctrl+Shift+N` to create a new untitled script.
* Save it with `Ctrl+S`, then hit `Ctrl+Alt+S` to *Save All*.
---
## Help & Docs
Quickly access documentation and help for R functions directly within RStudio. These shortcuts save time when you need to understand how a function works.
> 📚 **Learn more**: [RStudio Documentation](https://docs.posit.co/ide/user/)
### Quick‑reference
| Action | Keys |
| ----------------- | -------------------------- |
| Help on selection | `F1` |
| Search Help | `Ctrl + Alt + F1` |
Some laptops may require you to press the `Fn` key as well, as the F-keys are often assigned to some other action e.g. mute, sleep.
### Experiment
```{r}
# Place the cursor on this and press F1 to open its help page.
lm
# Some packages also offer package level documentation
dplyr
# Type :: after dplyr, then a pop-up list of package functions will appear.
# You can scroll through these with Up/Down, and press F1 on a hovered/selected item
# to view its documentation, even though you have not typed out that function name.
dplyr
```
---
## Additional Resources
### Official Documentation
- **[Complete Keyboard Shortcuts Reference](https://docs.posit.co/ide/user/ide/reference/shortcuts.html)** - Full list of all keyboard shortcuts
- **[Customizing Shortcuts](https://docs.posit.co/ide/user/ide/guide/productivity/custom-shortcuts.html)** - How to modify keyboard shortcuts
- **[RStudio User Guide](https://docs.posit.co/ide/user/)** - Comprehensive documentation
- **[Visual Editor](https://docs.posit.co/ide/user/ide/guide/documents/visual-editor.html)** - For visual markdown editing
### External Resources
- **[RStudio Cheatsheets](https://posit.co/resources/cheatsheets/)** - Quick reference PDFs
- **[RStudio Community](https://community.rstudio.com/)** - Forum for questions and tips
- **[RStudio Blog](https://posit.co/blog/)** - Latest features and tutorials
---
*Happy coding & shortcut‑smashing!*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment