Linux bash shell copy previous lines (not history) with keyboard

Solution 1:

As far as I know, neither of the "big three" terminal emulators can do that, but both screen and tmux offer a scrollback buffer you can access via keyboard.

GNU screen

screen's Prefix key is Ctrl+A per default, but all of that is customizable, so with a default config:

  1. Enter copy mode with Ctrl+A[.

  2. Move cursor around with vi motions: h, j, k, l, 0, ^, $, H, M, L, w, b, e, /, ?, g and G all work (see man screen under copy for more info on key bindings).

  3. Start selecting with Space, move around, and stop selecting with Space again. This will leave copy mode.

  4. Paste the selected text with Ctrl+A].

tmux

tmux behaves pretty much the same, except it uses some different keys per default:

  1. Enter copy mode with Ctrl+B[.

  2. Move cursor around with , , , , PgUp, PgDown etc. (see man tmux | less -p '^WINDOWS AND PANES' for the list of copy-mode bindings).

  3. Start selecting with Ctrl+Space, move around, and stop selecting with Meta+w. This will leave copy mode.

  4. Paste the selected text with Ctrl+B].

Solution 2:

Depends on the terminal you use. It is possible in shell mode in emacs.