Typically, when I want to perform an action on a file I discovered in the terminal after calling [ls], I would have to - with my mouse - highlight, left click, copy file name, type the command in then paste the file name again.

Is there a way to copy a certain string that was previously displayed in the terminal without using my mouse?


  • tmux and screen can both do this; I don't know off the top of my head of a way to do it without some sort of terminal multiplexer. But there might be some obscure terminal emulator that offered that natively.
    – Bandrami
    Mar 31, 2015 at 3:40
  • Look into xsel, I believe it can do this. Also xclipboard
    – texasflood
    Mar 31, 2015 at 9:00
  • By the way, you don't need to right click etc. When you select text it is automatically copied and can be pasted by clicking the middle mouse button.
    – terdon
    Mar 31, 2015 at 10:42

3 Answers 3

sudo apt-get install xclip

Create an alias:

 alias pbcopy='xclip -selection clipboard'

Use this command to copy

ls | pbcopy

(You can also use

ls <filename> | pbcopy 

to copy a specific filename. If you don't know what an alias is: http://www.hostingadvice.com/how-to/set-command-aliases-linuxubuntudebian/ )


If you're dealing with files discovered via ls, I usually find it easiest to tab-complete or glob them: if I want to act on report.tex, for example, it's faster to do vim re<TAB> than to copy-paste.

But copy-paste does have its uses, certainly. I do most of my work in tmux with vi bindings. If you have emacs bindings (the default) the approach is similar (below, C- means "press Control and then the key after the dash togehter", A- is similar but for Alt, and prefix is your prefix, probably C-b):

  1. In either, press <prefix>[ to enter copy mode
  2. Navigate to either end of the text you desire copied using arrow keys (always) or hjkl (vi mode only, probably?)
  3. If in emacs mode, press C-<Spaaaaaace> to begin the selection. If in vi mode, press <Spaaaaaace>.
  4. If in emacs mode, press A-w to copy the selected text. In vi mode, press <Enter>.
  5. Paste the selection with <prefix>] or use the command tmux show-buffer to output to stdout the copied text, useful for piping into commands and automation.

Tmux remembers what you copy when you copy other things. See the list of what's been copied with <prefix>#. <prefix>] pastes the most recent, but tmux show-buffer can take a -n <number> argument to output an older copy.

I don't know how to copy in screen off the top of my head, but it's a roughly similar process; I'm sure there's a wealth of blog posts about it on google.

Edit: I definitely recommend using a terminal multiplexer in general; you'll have another terminal at your disposal in three keystrokes regardless of terminal emulator or window manager. Muscle memory is good!


Urxvt has the excellent urxvt-perls collection of scripts that allow you to operate on text in the terminal without resorting to the rodent.

In addition to copy and paste, you can select URLs and open them in your browser or yank them to the clipboard, search the scrollback, and generally treat the text as if you were in your $EDITOR.

You must log in to answer this question.

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