Can one select terminal output via keyboard?
There's limited keyboard use when interacting with the scrollback buffer. It wasn't really designed to have "text editing" capabilities as that's what a text editor is for. Per the Terminal Users Guide, you only have the following "navigation" keys:
Action |
Keyboard Shortcut |
Scroll to top |
⌘ CommandHome |
Scroll to bottom |
⌘ CommandEnd |
Page up |
⌘ CommandPgUp |
Page down |
⌘ CommandPgDn |
Line up |
⌥ Option⌘ CommandPgUp |
Line down |
⌥ Option⌘ CommandPgDn |
Set Mark |
⌘ CommandU |
Unset Mark |
⇧ Shift⌘ CommandU |
As you can see, there's no lateral movement available - you can select whole lines, but not individual characters. Terminal is, after all, a GUI app and it appears to be intended to have these "advanced" text selection done with a mouse.
Redirect your output
Real World Example:
Using tools like grep
, cut
or awk
, you can get just the data you're looking for. For example, suppose we want to find our what the router IP address is. We could issue the command ipconfig getpacket en0
. From there you'll get all of the DHCP info that was assigned to your network interface en0
. You can then search through and find what you're looking for like the router’s IP. There are easier ways to extract this info:
Using grep
we can narrow it down to a single line:
ipconfig getpacket en0 | grep router
router (ip_mult): {192.168.0.1} ← Result
If you're trying to capture certain things you can rerun your command and with a little creative use of the cut
command, we can "extract" the IP
ipconfig getpacket en0 | grep router | cut -d "{" -f2 | cut -d "}" -f1
192.168.0.1 ← Result
Finally, you can send it directly to your clipboard by redirecting/piping that output to your clipboard via pbcopy
ipconfig getpacket en0 | grep router | cut -d "{" -f2 | cut -d "}" -f1 | pbcopy
← Blank line result! It's in your clipboard!
Granted, this example is a bit extreme on the pipe (|
) usage and you should be aware that every pipe creates a sub-process; in other words, it's very inefficient! However, for small items such as this, this really isn't an issue.
Using tmux
tmux
is an excellent tool and I agree with everything said in the answer that suggested it, however, there's some missing context that should be addressed:
tmux's power comes from the ability to keep a session alive in the event of a disconnect; it should be installed on the server for best results. Yes, you can run it locally, but it's like using a cannon to kill a mosquito.
Use builtin screen
Again, I'm a huge fan of tmux
, but instead of installing something to only use 10% or less of it's features, use screen
that's included with macOS. It has the same features and includes the navigation keys to mark and copy text. Here's a short sampling:
Action |
Key Binding |
move the cursor left |
h or ^ Controlh or ← |
move the cursor down |
j or ^ Controlj or ↓ |
The full (lengthy) list can be found at the above link.
somecommand | pbcopy
.screen
or iTerm2, there are ways to select text using the keyboard (perhaps not faster than using the mouse though).