Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 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

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 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:


   Enter  copy/scrollback mode. This allows you to copy text from the cur-
   rent window and its history into the paste buffer. In this mode  a  vi-
   like `full screen editor' is active:
   Movement keys:
     h, j, k, l move the cursor line by line or column by column.
     0,  ^  and  $  move to the leftmost column, to the first or last non-
       whitespace character on the line.
     H, M and L move the cursor to the leftmost column of the top,  center
       or bottom line of the window.
     + and - positions one line up and down.
     G moves to the specified absolute line (default: end of buffer).
     | moves to the specified absolute column.
     w, b, e move the cursor word by word.
     B, E move the cursor WORD by WORD (as in vi).
     C-u  and  C-d  scroll  the display up/down by the specified amount of
       lines while preserving the cursor position. (Default: half  screen-
     C-b and C-f scroll the display up/down a full screen.
     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
   paste  buffer. If no parameter is given the user is prompted for a sin-
   gle register to paste.  The paste buffer can be filled with  the  copy,
   history  and  readbuf commands.  Other registers can be filled with the
   register, readreg and paste commands.  If paste is called with a second
   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.
   Note, that "paste" uses a wide variety of resources: Whenever a  second
   argument  is  specified  no  current  window is needed. When the source
   specification only contains registers (not the paste buffer) then there
   need not be a current display (terminal attached), as the registers are
   a global resource. The paste buffer exists once for every user.

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

Now all that said, and you do mention 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

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

You must log in to answer this question.

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