Shell command to put recent line of output onto current command line

command historycommand lineoutputshell

Shells have a useful feature where pressing the keyboard UP arrow replaces the contents of the current command line with the previous command that was entered. (And if you do it again, the command before that, and so on.)

I would like a similar feature, where pressing (some other button) replaces the contents of the current command line with the last line of output from the previous command (and if you do it again, the previous line of output , and so on.)

Is there any shell that provides a feature like this?

The motivation is that you often want to run a command, and then run another command with part of the previous commands's output as input. (No, I don't want to do anything involving piping the output of one command into the next. I'm looking for an interactive experience, where you run the first command, see its output, and then get a line of that output into your next command, without having to reach for the mouse to do a clumsy copy-and-paste.)

Best Answer

If you run your shell inside screen, you can use screen’s scrollback mode to achieve this. Using the default key bindings:

  • CtrlAEsc enters scrollback mode;
  • movement keys move up and down;
  • Y copies the current line to the paste buffer and leaves scrollback mode;
  • CtrlA] pastes the buffer.

The documentation (run man screen) gives lots more detail and describes the other features of scrollback mode (including searches, partial copies etc.).

tmux no doubt has similar features.