How to copy and paste into and out of a terminal

The problem

In the Linux desktop environment, there are two methods of copying and pasting. Highlight-to-copy/middle-click-to-paste and CTRL+C-to-copy/CTRL+V-to-paste. The highlighting copy/paste is easy to get text into and out of the terminal but CTRL+C is already mapped to a different function and CTRL+V doesn’t do anything.

This leads to the problem of not being able to copy/past text between applications and the terminal.

The solutions

CTRL+V and CTRL-V in the terminal.

There are alternative shortcuts assigned to these functions in the terminal. You just need to press SHIFT at the same time as CTRL:

Copying large blocks of text

When you need to copy very large amounts of text use the xclip utility. xclip will accept any amount of text from standard input and put it into your copy buffer.

For example, if you have a text file called report.txt and you want copy all of the text use the following command:

xclip -selection clipboard report.txt

This is a good candidate for an alias. I have the following in my .bashrc:

alias xclip=/usr/bin/xclip -selection clipboard

xclip will also accept standard input if what you want to copy something that isn’t in a file such as the output of a command:

ps auxf | xclip -selection clipboard

Pasting isn’t usually a problem but xclip will also print the contents of the copy buffer to standard output (stdout) with the -o option e.g.:

xclip -selection clipboard -o