Terminal

The RStudio terminal provides access to the system shell from within the RStudio. It supports full-screen terminal applications such as vim, Emacs, and tmux as, well as regular command-line operations with line-editing and shell history.

Potential uses of the shell including advanced source control operations, execution of long-running jobs, remote logins, system administration of Posit Workbench or RStudio Server, and the aforementioned full-screen terminal programs.

This article does not attempt to cover fundamentals on when and how to use a system shell. Instead, it focuses on the features, design, options, and limitations of the RStudio terminal feature itself.

Unlike most RStudio features, the nature of the terminal leads to several platform-specific differences. These primarily break down into two categories: Windows, and everything else (Mac, Linux, and Server). Windows provides emulators for unix-based terminals such as Git Bash, Windows Subsystem for Linux, or PowerShell.

Getting started

The Terminal tab is next to the Console tab. Click on the Terminal tab and a new terminal session will be created (if there isn’t one already).

  • If the tab isn’t visible, display it via Tools > Terminal > Move Focus to Terminal (or use the keyboard shortcut Shift+Alt+M or Shift+Option+M on Mac).

  • The full list of terminal-related keyboard shortcuts is in the Reference: Keyboard Shortcuts Terminal section. Most terminal-related commands can be found on the Terminal drop-down menu in the terminal pane, and on the Tools > Terminal menu.

Here’s a terminal with the output of simple command:

screenshot of terminal showing simple output

The visual appearance of the terminal is controlled via the Tools > Global Options > Appearance settings.

Multiple terminals

RStudio supports multiple terminal sessions. To start another terminal session, use the New Terminal command on the Terminal drop-down menu, or Alt+Shift+R.

screenshot showing terminal menu opened Each terminal session is independent, with its own system shell process and buffer. Switch between them by:

  • Using the arrows next to the drop-down menu
  • Clicking on the terminal’s name in the drop-down
  • Using the Previous Terminal/Next Terminal drop-down menu commands. The drop-down button shows the name of the currently selected terminal session (“Terminal 1” in the following screenshot):

screenshot showing navigation between multiple terminals

Terminal names

Terminal sessions have a default name, “Terminal 1”, “Terminal 2”, and so on. Rename the current terminal via the Rename Terminal command on the drop-down.

Thus, in your workflow, you can create multiple terminals and give them meaningful names to simplify navigation. A collection of named terminals, or “terminal set”, is generally associated with an RStudio project.

An alternative (or complementary) approach is to use a terminal multiplexer such as Tmux or Screen.

Execute in terminal

When editing files in the RStudio editor, any selection (or the current line if nothing is selected) can be sent to the active terminal via Ctrl+Alt+Enter (Cmd+Alt+Enter on the Mac). If a single-line was sent to the terminal the cursor will advance automatically to the next line, allowing single-stepping.

In following example:

  1. A simple Python source file was loaded
  2. A Python interpreter started in the current terminal
  3. Ctrl+Alt+Enter used to step through the script:

screenshot of a simple Python REPL example

This is just an example; for more powerful integrated Python support look at the reticulate package.

If a shell file is being edited (*.sh), Ctrl+Enter (Cmd+Enter on Mac) can be used to single-step, as can the Run toolbar button. This will also work on Windows, but no check is done that the shell-type loaded in the active terminal session matches the commands being sent.

screenshot example of running a shell script

The Run Script button does the same as prior versions of RStudio and will save and run the entire script via system() in the R Console. This button does not appear in Windows.

Busy terminals

Programs running in a terminal do not block the rest of the RStudio user-interface, so work can continue working in RStudio even when one or more terminal sessions have a child process running.

A terminal with child processes is considered busy by RStudio, and will have:

  1. (busy) next to the terminal name.
  2. The close x changes to a STOP button.

The RStudio integrated terminal, highlighting the 'x' button to close.

screenshot showing a busy terminal

screenshot showing a busy terminal

This “busy detection” feature is only available on Mac, Linux, and Posit Workbench / RStudio Server and not on Windows.

Closing terminal sessions

To close a terminal session, use the Close Terminal command on the Terminal drop-down menu, click the x on the far-right of the Terminal pane toolbar, or type exit from within the shell itself.

As discussed earlier, if a terminal is currently busy, the x changes to a STOP button.

Clicking the STOP button will trigger the Interrupt Current Terminal command, sending an interrupt to the terminal; this may close the currently running program (the program itself decides what to do when receiving an interrupt), but will not close the terminal.

Finally, if the terminal feature it not useful to your workflow, you can remove the tab altogether by clicking the x on the Terminal tab itself. This will close all running terminal sessions, including any custom-named terminal sessions. Subsequent instances of RStudio will not show the Terminal tab: to bring it back, use Tools > Terminal > Move Focus to Terminal (keyboard shortcut: Shift+Alt+M or Shift+Option+M on Mac).

Terminal buffer

The terminal buffer keeps the last 1000 lines of output when running non-full-screen terminal programs. Scroll through this buffer using the scrollbars (via the mouse) or a mouse-wheel if the system supports it. Full-screen programs typically have their own built-in buffers and navigation conventions.

Clearing the display with the native shell command (e.g. clear for a Posix-style shell, cls for a Windows Command Prompt, or Clear-Host for PowerShell) will only clear the visible portion of the buffer. To clear the entire buffer, use Ctrl+L when focus is in the terminal.

The mouse can be used to select text in the terminal so it can be copied & pasted elsewhere.

To send the entire terminal buffer to a new RStudio editor buffer, use the Copy Terminal to Editor command on the terminal drop-down menu.