Take the 2-minute tour ×

I like to copy things from the terminal, but I don't like to have to move to the mouse. How do you select text in Terminal.app using the keyboard?

For example, while in Terminal one might like to be able to move to a point on a line and then highlight a portion of text with left and right arrow keys, which one could then cut or copy. Is this possible?

share|improve this question
add comment

2 Answers

If you want to copy the output from a command, you can use pbcopy. To get specific output, use grep.

For example, to copy the entire output of netstat -an:

netstat -an | pbcopy

To only copy the output of open ports (listening):

netstat -an | grep LISTEN | pbcopy

Terminal.app itself does not have a way to use the keyboard to select text, but using a terminal multiplexer like screen or tmux, you can do so in "copy mode". Screen is installed on OS X by default, and tmux can be installed through a third party tool such as Homebrew or Macports. The man page for both programs describes copy mode and how to select text to copy to the clipboard. In the screen man page, more information about navigating copy mode can be found in the CUSTOMIZATION section under copy. An excerpt below:


   like `full screen editor' is active:
   Movement keys:
       whitespace character on the line.
       or bottom line of the window.
     + and - positions one line up and down.
     | moves to the specified absolute column.
     w, b, e move the cursor word by word.
     g moves to the beginning of the buffer.
     % jumps to the specified percentage of the buffer.

This still doesn't interact with the system's clipboard, so you would have to copy the output in some way, perhaps by echoing the paste buffer contents to pbcopy. Example workflow:

(C-a [ is "hold control press A and then hit [", same for ]. see screen man page.)

  1. Start screen and do work there.
  2. Enter copy mode: C-a [
  3. Navigate using vi keybindings (or set to use emacs bindings per the man page).
  4. Start text selection with spacebar.
  5. Hit enter to copy selection to paste buffer. This exits copy mode.
  6. Paste the buffer with C-a ]

    echo 'C-a ]' | pbcopy

(don't literally type C-a ], use the keybinding as described above, use quotes where necessary)

The text you selected should be in the system clipboard and you can paste it into another window. For example the following:

   paste [registers [dest_reg]]

   Write  the  (concatenated)  contents  of the specified registers to the
   stdin queue of the current window. The register . is treated  as  the
   history  and  readbuf commands.  Other registers can be filled with the
   argument,  the  contents  of the specified registers is pasted into the
   named destination register rather than the window. If .  is  used  as
   the  second  argument,  the  displays  paste buffer is the destination.
   argument  is  specified  no  current  window is needed. When the source
   specification only contains registers (not the paste buffer) then there

Was from the screen man page from doing the above :).

Now all that said, and you do mention Terminal.app specifically, a new feature of iTerm2 version 0.20.20101102 which is in alpha testing shows:

  • Mouseless Selection: Do you often copy-paste text within the same window? Now you can do it without using the mouse. Open the findbar with cmd-f and search for the text you wish to copy. When part of the text is matched, use the tab key to expand the selection to the right by a full word and shift-tab to expand it left to the previous word boundary. It's automatically copied to the clipboard, or you can use opt-Enter to paste the selection immediately.

While is it alpha, I've been using it for all of my Terminal work for about a month and a half and it is quite stable.

iTerm2 home page

share|improve this answer
pbcopy is pretty cool! –  warren Sep 15 '10 at 13:38
I guess installing some other terminal application allows for an easier usage scenario. But the 50 points of the bounty are effectively less than 1 point per line, so you deserve it for sure! ;-) –  Arjan Sep 16 '10 at 17:06
Aw shucks, thanks :). This was also an exercise in learning for me, to better interact with clients that use tmux (which I prefer over screen, but its not installed on OSX by default). –  jtimberman Sep 16 '10 at 22:43
add comment

If what you want to copy is the commands you've entered at a Bash prompt, you can use Bash's readline line editing features:

Here's an example in the (default) emacs mode:

  • Press Ctrl-r and type part of the command you'd like to recall from history
  • Press Ctrl-u to cut (kill) that line into the kill buffer
  • Press Ctrl-y to paste (yank) the kill buffer onto the command line (you could press it multiple times to have repeated copies)

There are many possibilities including affecting parts of the line or particular words, etc.

See man bash in the "Readline" section and man readline for more information.

share|improve this answer
Yes, but this works only in the command line. Things in the kill buffer are not transferred to the Mac clipboard. –  hekevintran Apr 22 '10 at 4:02
add comment

Your Answer


