8

Often I like to copy output from the terminal into the clipboard.

Currently I'm using the mouse. Select the text so that it is marked. Then "command" plus "c".

In a text-editor or text-processing software you can use the "shift" key (keep it pushed) together with the arrow keys for selecting text. So one needs the mouse less and can keep the hands on the keyboard.

On the terminal that doesn't work. Keeping the "shift" key pushed and pushing the arrow keys just creates this: ;2C;2B;2A;2D;2D;2D;2D;2D

Hexadecimal numbers obviously. Does anyone knows the meaning?

But my actual question is:

Can one select terminal output via keyboard?

Similar to what I have described concerning text-editors.

4
  • I'm not aware of any way to select a particular piece of output from Terminal, but shift+cmd+up will select all output of the previous command. Aug 18, 2016 at 16:42
  • 4
    This doesn't answer your question directly, but you can copy the output of a command to the clipboard by doing somecommand | pbcopy. Aug 18, 2016 at 17:21
  • Also, if you use something like screen or iTerm2, there are ways to select text using the keyboard (perhaps not faster than using the mouse though). Aug 18, 2016 at 17:25
  • @leekaiinthesky : Thanks a lot. :) That's indeed very handy too know ...
    – mizech
    Aug 19, 2016 at 5:42

2 Answers 2

4

The terminal multiplexing program tmux has configurable keyboard commands for selecting text for the purpose of copying and pasting. tmux is a very powerful addition to any terminal. It has a high learning curve, but if all you want to do is copy and paste you can learn how in a matter of moments and it will stay completely out of your way until you are ready to take the next step. tmux can be installed directly, or using a package manager such as Macports or Homebrew.

2

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

  • Redirect your output to a file:

    mycommand > output.txt         ← Captures all output to the file (overwrites file)
    mycommand >> output.txt        ← Captures all output and appends to the file (no overwrite)  
    

  • Pipe your output to pbcopy (macOS clipboard)

    mycommand | pbcopy
    

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.

2
  • 1
    I like this I’m gonna show my boss this one. I tried it but the command is long and alot to remember . Can you make a shortcut or something? May 13 at 18:56
  • Glad I could help you out @DougMasters
    – Allan
    May 13 at 23:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .