21

This question is about configuring the R console to behave like a bash shell when it comes to navigating the command history. It is somewhat related to the ?history. For brace-enclosed multi-lines, I'd like to configure the command history navigation of R to be similar to bash.

Presently when running R in an xterm under Linux, using the up-arrow to navigate the command history causes each previous line to be recalled one by one, even if a set of lines had been enclosed in braces. This occurs, for example, when copy/pasting a multi-line function from a text editor into the R console. Not so with bash. Here is an example of how bash functions in this regard:

In a bash shell within an xterm under Linux, after typing the following five lines...

a=1 
{
x=1
y=1
}

... the first press of the up-arrow will recall a single line reformulation of the brace-enclosed commands, like this ...

{ x=1; y=1; }

... and the second press will recall this ...

a=1

It seems that in R, the up-arrow navigates backwards one line at a time, regardless of encapsulation. Is there a way to configure R so that it's command history navigation functions like bash's?

mike
  • 221
  • 1
  • 7
  • 4
    In case you are an Emacs user (or would contemplate becoming one), it's prob. worth mentioning that ESS (Emacs Speaks Statistics) does just what you're asking for. And yes, it's *extremely* handy. – Josh O'Brien Apr 25 '13 at 23:24
  • 7
    As does RStudio, if I understand the question correctly. – Ari B. Friedman Apr 25 '13 at 23:39
  • 2
    (OK, on re-reading your question, ESS actually does something a bit different (and nicer), recalling the entire pasted-in code block, whether or not it's enclosed in braces, or composed of several expressions, or whatever.) – Josh O'Brien Apr 25 '13 at 23:52
  • @AriB.Friedman, are you sure? The RStudio server that we have implemented specifically lacks such functionality (and is consequently quite frustrating to use) – Ricardo Saporta Apr 26 '13 at 04:17
  • 1
    @RicardoSaporta Are you using the latest version? I just tried it with RStudio Server (0.97.248) and it behaves as JoshO'Brien describes: recalls everything that was pasted in at a time with a single press of the uparrow. – Ari B. Friedman Apr 26 '13 at 05:40
  • 3
    The alternative is to find a text editor which supports "piping" to the console, which several do. – Carl Witthoft Apr 26 '13 at 12:22
  • @AriB.Friedman, the Rstudio does it only when sourcing (from editor) multi line selection (beeing one function) into console. When typing directly into console it works line by line. – Petr Matousu Feb 21 '15 at 16:08
  • @PetrMatousu Not exactly. Try typing directly into the console, but hit Shift-Enter instead of Enter. Then when you arrow up it will pull up the whole expression at once. – Ari B. Friedman Feb 21 '15 at 16:34
  • 1
    @AriB.Friedman I see it now. Nice feature. But standard enter makes it same as any other linux R console - that I wanted to say. – Petr Matousu Feb 21 '15 at 16:44
  • @AriB.Friedman The same way as in Rstudio, but different keystrokes, in the linux terminal R session, C-V, C-J. Thanks to your comment. – Petr Matousu Feb 21 '15 at 19:29

2 Answers2

1

You could use rlwrap. I use it for other console programs and it works very well. You will need to prepend the R command with the rlwrap binary and then your history lines can be restored in a number of ways, including multi-line matching.

Robert Kubrick
  • 8,413
  • 13
  • 59
  • 91
1

Workaround for Linux/Unix

Similarly as in Rstudio (thanks to Ari B. Friedman comment), where user in R console is using ShiftEnter to bypass RETURN, you can start newline (in R terminal) without accepting newline command using Ctrl-VCtrl-J. This way the multi-line command will be accepted into history as one-liner with line-feeds instead of enters and you will even have the chance to edit it. You can even manage in your .inputrc file to have custom combination for this action.

I do not think direct reconfiguration of R is possible.

Readline man page may help more.

Petr Matousu
  • 3,120
  • 1
  • 20
  • 32