How to quickly get the previous line output in a terminal without using a mouse?

Solution 1:

If you don't mind a little obscenity (I don't), you might want to use the fuck, a tool that does exactly what you asked for.

Well, not exactly, but it solves the same problem. Instead of just getting the latest line, it tries to match the last command you typed.

The Fuck tries to match a rule for the previous command, creates a new command using the matched rule and runs it.

The examples shown in the repository show several of the scenarios you mentioned.

enter image description here

Solution 2:

The short answer: no, probably you have no available method for doing this.

The long answer:

  • while it is possible to have a terminal emulator which allows you to "just" read back the screen contents and edit them, in practice that isn't done often due to concerns that some unwanted program could snoop on your keyboard entries.
  • a shell could be designed capture the output of your commands and allow you to edit that (just like your command history). It would get complicated if the command was something like vi (where the output isn't nice line-oriented text). bash doesn't do that, anyway.

In practice, what people do to work around this limitation is use other programs to capture the command-output, and edit that. But it won't solve your immediate problem.

For example, I use vi-like-emacs to issue ad hoc shell commands. If those do not require further input, it works well to run those in a window, capturing the output of the command — and editing the output as needed to refine the command. Emacs also allows you to do something like that, as well as vim.