With BASH after "scrolling" up to a previous command... how to then move on to the next in this history?

Running the command with Ctrl+o instead of Enter will run a command from history and then queue up the next one instead of returning to the front of the bash history.


Jon Reinhold's answer is great, but there's is a much more powerful solution that I'll suggest. I also have a comment about a gotcha in Jon's answer, but I don't have enough reputation to be able to comment directly, so @Jon Reinhold, if you read this, please address my comment to you below.

Bash includes a command fc, which takes as parameters line numbers of the bash history list. It then opens your default editor with those lines as text. At that point, you can optionally edit the lines. Then, when you exit the editor, bash executes those lines!

An example of an edit you might want to make is append to all but the last line something like "; read -p"next ...". That will cause bash to execute each line, and prompt you before continuing.

Comment for Jon Reinhold: Great answer, but you should qualify it because if the user has set bash variable HISTCONTROL to include erasedups, then after performing C-o the user will be confused because instead of the expected next command in the history being displayed, the one after that will be displayed. This is because bash has deleted the original instance of the executed command, and thus all the commands afterwards have shifted up one line, ie. to one lower index number in the history list.


Another way to accomplish your desired behavior would be to get familiar with the bash readline shortcuts (of which CTRL-o is one I believe) and bash history searching.

History search

CTRL-r takes you into bash command history search, where you can begin typing the command you are searching for and bash will autocomplete the command for you. The autocomplete functionality is really pretty good. When the command you want to run is on the input line, you can ENTER to run the command, or press CTRL-e to move the cursor to the end of the command line and exit history search mode.

The cool thing with CTRL-e at this point is that the history buffer is set contextually to this command. The next and previous commands now are the ones ran just before and after the line that the history search located for you. You can press the up or down arrows and grab the next command.

History search is very powerful and a great way to avoid using the up arrow to get way back to the command in the first place. A quick history search can save lots of time manually searching through the history and then you can proceed with CTRL-o like Jon pointed out above.

Readline Short Cuts

If you are looking to up your overall bash-fu, I would recommend giving the readline shortcuts for the arrow keys a shot. You might find that they are more convenient and allow you to increase your typing speed, but of course YMMV. Here are a few more:

  1. CTRL-n for the next command (down arrow equivalent)
  2. CTRL-p for the previous command (up arrow equivalent)
  3. CTRL-b back a character (left arrow equivalent)
  4. CTRL-f forward a character (right arrow equivalent)

These readline shortcuts (along with CTRL-a beginning of line and CTRL-e end of line) will increase your speed and efficiency at the command line imo.