The R Programming Language: The command-line editor

0

Preliminaries

When you work with R under UNIX and have the GNU readline library available during compilation, you gain access to a powerful inbuilt command-line editor. This editor facilitates the recall, editing, and re-submission of prior commands. Note that different versions of readline may be utilized, particularly on macOS. To identify the version in use, run `extSoftVersion()` in an R session.

If needed, the inbuilt command-line editor can be disabled, which is particularly useful for integration with tools like ESS, using the startup option `--no-readline`. For Windows versions of R, command-line editing is somewhat simpler, with details available under the 'Console' section in the GUI's 'Help' menu and in the README.Rterm file for editing under Rterm.exe.

When working with GNU readline capabilities, various functions become available, many of which are documented in `man readline` or `info readline` on your system. These functions often involve the use of Control (C) or Meta (M) characters. Control characters, obtained by holding down CTRL, are denoted as C-m, while Meta characters, achieved by holding down META, are written as M-b. If your terminal lacks a META key, you can still input Meta characters using two-character sequences starting with ESC. For example, to enter M-b, type ESC b. Note that case sensitivity applies to Meta characters.

Avoid resizing the terminal window when using readline, as not all versions support this feature.

Editing Actions

The R program maintains a history of command lines, including erroneous ones. In Emacs-style command-line editing, characters typed during this phase are inserted into the command being edited, displacing characters to the right of the cursor. In vi mode, character insertion mode is started by M-i or M-a, characters are typed, and insertion mode ends with another ESC.

Pressing RET at any time submits the command. Here is a summary of other editing actions:

Command-Line Editor Summary

Command Recall and Vertical Motion

  • C-p: Go to the previous command (backwards in history).
  • C-n: Go to the next command (forwards in history).
  • C-r text: Find the last command with the specified text string.

Horizontal Motion of the Cursor

  • C-a: Go to the beginning of the command.
  • C-e: Go to the end of the line.
  • M-b: Go back one word.
  • M-f: Go forward one word.
  • C-b: Go back one character.
  • C-f: Go forward one character.

Editing and Re-submission

  • text: Insert text at the cursor.
  • C-f text: Append text after the cursor.
  • DEL: Delete the previous character (left of the cursor).
  • C-d: Delete the character under the cursor.
  • M-d: Delete the rest of the word under the cursor and "save" it.
  • C-k: Delete from cursor to the end of the command and "save" it.
  • C-y: Insert (yank) the last "saved" text.
  • C-t: Transpose the character under the cursor with the next.
  • M-l: Change the rest of the word to lowercase.
  • M-c: Change the rest of the word to uppercase.
  • RET: Re-submit the command to R. The final RET terminates the command line editing sequence.
Customization of readline key bindings is possible via a `~/.inputrc` file, allowing tailored adjustments, which can be conditioned on the application, such as R, for specific configurations. For example:

$if R
"\C-xd": "q(’no’)\n"
$endif

By mastering these command-line editor functionalities, you can significantly enhance your efficiency and effectiveness when working with R in a terminal environment.

Post a Comment

0Comments
Post a Comment (0)