Text Editor

RStudio’s source editor includes a variety of productivity enhancing features including syntax highlighting, code completion, multiple-file editing, and find/replace.

RStudio also enables flexible execution of R code directly from the source editor. For many R developers this represents their preferred way of working with R. Working in the source editor makes it much easier to reproduce sequences of commands and to package commands for re-use as a function. These features are described in the Executing Code section of the user guide.

The core features listed below are accessible via the Code Tools “Magic Wand” dropdown menu in the Source pane.

Screenshot of the Code Tools dropdown

Most of these features have corresponding keyboard shortcuts, which also means they can be accessed via the Command Palette (Ctrl/Cmd + Shift + P).

A screenshot of the Command Palette with the search limited to 'Rename' symbol in scope

Code completion

RStudio supports the automatic completion of code using the Tab key. For example, start typing return as ret then Tab and RStudio will automatically complete the full name of the function or object.

Screenshot of RStudio's autocompletion of the 'return' function

Code completion also works in the console, and more details on using it can be found in the console Console documentation.

Extract Function

RStudio can analyze a selection of code from within the source editor and automatically convert it into a re-usable function. Any “free” variables within the selection (objects that are referenced but not created within the selection) are converted into function arguments:

Screenshot of the Code Tools > Extract Function button

Find and Replace

RStudio supports finding and replacing text within source documents:

A screenshot of the Find and Replace text interface within a source file.

Find and replace can be opened using the Ctrl+F shortcut key, or from the Edit > Find... menu item. Replacement can be done all at once or by stepping through each instance.

Comment/Uncomment

RStudio provides the ability to comment and uncomment entire selections of code using the Code > Comment/Uncomment Lines menu item or with the Command+Shift + C keyboard shortcut.

Screenshot of the 'Comment/Uncomment Lines' button

Multiple Cursors

  • Press Ctrl + Alt + {Up/Down} to create a new cursor in the pressed direction,

  • Press Ctrl + Alt + Shift + {Direction} to move a second cursor in the specified direction,

  • Use Alt and drag with the mouse to create a rectangular selection,

  • Use Alt + Shift and click to create a rectangular selection from the current cursor position to the clicked position.

Screenshot of multiple cursors across 3 lines of code

Use the “All” button on the Find/Replace toolbar to create cursors at each instance of a word.

Screenshot of 3 cursors, one at each instance of the searched term in the current R script.

Move lines up/down

Text or code in source files can be moved up or down by the Alt+Up/Down (Option+Up/Down on Mac). This applies to individual lines of code where the cursor is, as well as to multiline selections.

A screencast of an example line of text being moved up with the move lines up/down shortcut

Copy lines up/down

Text or code in source files can be copied up or down with the Shift+Alt+Up/Down (Cmd+Option+Up/Down on Mac) shortcut. This applies to individual lines of code where the cursor is, as well as to multiline selections.

A screencast of an example line of text being duplicated across multiple lines with the copy lines up/down shortcut

Keyboard Shortcuts

Beyond the keyboard shortcuts described above, there are a wide variety of other shortcuts available. Some of the more useful ones include:

  • Ctrl+Shift+N — New document

  • Ctrl+O — Open document

  • Ctrl+S — Save active document

  • Ctrl+1 — Move focus to the Source Editor

  • Ctrl+2 — Move focus to the Console

A list of all shortcuts in the Keyboard Shortcuts.

Back to top