Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I copy the output of a command directly into my clipboard?

How can I pipe the output of a command into my clipboard and paste it back when using a terminal? For instance:

cat file | clipboard 
like image 657
Legend Avatar asked Feb 27 '11 02:02

Legend


People also ask

How do I copy Linux output to clipboard?

To copy text into a clipboard, you need to use a command like $ echo "Hello World" | xclip - i . It stores the text into a clipboard. To paste the output, we need to use the $ xclip - o command, and it will copy the text from the clipboard and paste out the stored output Hello World on the screen.

How do I copy a Output command?

To copy the command prompt output to the clipboard, use one of the methods. Using Keyboard: Press Ctrl + A to select all text, and press ENTER to copy it to the clipboard. Using the Edit menu: Right-click the Command Prompt title bar → Edit → Select All. Repeat the same, and this time, select Copy from the Edit menu.

How do I copy terminal output to a file?

the shortcut is Ctrl + Shift + S ; it allows the output to be saved as a text file, or as HTML including colors!


1 Answers

I always wanted to do this and found a nice and easy way of doing it. I wrote down the complete procedure just in case anyone else needs it.

First install a 16 kB program called xclip:

sudo apt-get install xclip 

You can then pipe the output into xclip to be copied into the clipboard:

cat file | xclip 

To paste the text you just copied, you shall use:

xclip -o 

To simplify life, you can set up an alias in your .bashrc file as I did:

alias "c=xclip" alias "v=xclip -o" 

To see how useful this is, imagine I want to open my current path in a new terminal window (there may be other ways of doing it like Ctrl+T on some systems, but this is just for illustration purposes):

Terminal 1: pwd | c  Terminal 2: cd `v` 

Notice the ` ` around v. This executes v as a command first and then substitutes it in-place for cd to use.

Only copy the content to the X clipboard

cat file | xclip 

If you want to paste somewhere else other than a X application, try this one:

cat file | xclip -selection clipboard 
like image 128
Legend Avatar answered Sep 21 '22 12:09

Legend