2

On almost all modern command-line environments I've used, you can hit up arrow to go back a few commands in your history and re-run an earlier command. But on some command-line environments I've used, at the next command prompt after doing that, you can also hit down-arrow to browse your history from the place where the previous command appeared in the history.

For example, suppose you do this:

$ vi mysource.c  
$ make  
$ make test  

Then suppose your test fails and you want to go back through these steps again, so you hit up-arrow 3 times to get back to vi mysource.c, and hit return to run that. Then, at the next prompt after that, I want to be able to hit down-arrow once to get to make. And at the next prompt after that, I want to hit down-arrow once to get to make test.

I don't know if it comes down to Mac OS X's default bash settings, or default Terminal.app settings, or what, but this "hit the down arrow at a new prompt to browse history down from the last place you used the history" functionality is not enabled by default.

I browsed the bash(1) man page and found:

       forward-search-history (C-s)

...but ^S didn't seem to do anything. Not sure if it was being swallowed up by Terminal as an old-school XOFF or something (although it didn't seem to pause terminal output).

A few other notes:

  • I'm on Mac OS X Snow Leopard 10.6.6.
  • I'm already aware of lots of other fun ways to work with the shell history, it's just this one particular thing that I'm trying to get working right now.
  • Although any pointers in the right direction would be helpful, solutions known to work on 10.6.x's bash in Terminal.app would be super extra helpful.
2
  • "On almost all modern command-line environments I've used" Does that include bash on another platform? i.e., do you know that bash can behave this way? Because I've never seen it do this. Down-arrow (and ^s) work once you've started browsing the history with Up-arrow (or ^r), but I don't recall seeing an option to have bash keep track of which history command was executed on the previous command line.
    – Chris Page
    Aug 21, 2011 at 11:28
  • 1
    @Chris Note that I said almost all have up-arrow history, but some have the form of down-arrow history I'm looking for. As I recall, the Windows command prompt has this, and I'm sure tclsh on Windows has this. I'm not sure if I've seen traditional Unix shells do this anywhere, but I'd probably look at bash under Windows/cygwin if anywhere. I don't know how much of this functionality is part of the shell itself or somehow part of the terminal emulator (or cmd.exe), because for example tclsh does this in Windows but seemingly not in Mac OS X.
    – Spiff
    Aug 22, 2011 at 0:15

2 Answers 2

1

^S does the opposite of ^R (after you've done ^R). You may need to do stty -ixon to disable the XOFF behavior.

The closest to what you're looking for is provided by ^O (readline operate-and-get-next). Press up-arrow to retrieve a previous command and press ^O instead of Enter. The command will be executed. When the prompt is issued again, the next command in the history will be put on the command line. You can use arrows, Enter or ^O at this point.

2
  • This isn't working for me at all, even though bash's bind -p built-in tells me that C-o is bound to operate-and-get-next. Even tried it from the Guest account to make sure I hadn't screwed up my Terminal or bash environment at some point in the distant past.
    – Spiff
    Feb 26, 2011 at 7:22
  • 2
    Never mind, I figured out that ^O was getting eaten by the tty as the "discard" character. stty discard "^-" fixed it. I tried out the operate-and-get-next functionality, and decided it's not close enough to what I'm looking for. The search continues...
    – Spiff
    Feb 26, 2011 at 7:38
1

Depending on your version of stty, you may need "stty flush undef". (Currently the OS X stty man page documents "flush", but the stty -a output still displays the value of "discard".)

Use ^o to execute a command from the history when you think you might want the next command too. If you do want it, just repeat the ^o. If you don't want it after all, then you move around in the history from that point with ^p, ^n, ^s, etc., or clear the command line with ^u.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .