How-to: Cat a file to your clipboard for pasting.

Write tutorials here
There are more tutorials here http://community.linuxmint.com/tutorial/welcome
Forum rules
Do not start a support topic here please. Before you post please read this

How-to: Cat a file to your clipboard for pasting.

Postby optimize me on Tue Aug 11, 2009 9:23 pm

I just found a solution to a problem that's been nagging me for a while. I have a series of text files that I access often and paste the contents here & there, but going through the trouble of typing out "gedit /path/to/this/buried/file" is almost as bad as opening Nautilus, navigating to the file, and double-clicking to bring up a text editor, then selecting all, then going back to my original application and pasting it in.

A quick Google search turns up the application xclip, and a lot of information about how Linux uses three clipboards, and they're all for different things, and.... yawn.

Anyways, with the right switches, xclip can read from STDIN and put the information into your x clipboard ready for pasting via the mouse or the keyboard shortcut. Very handy!

First I installed xclip
Code: Select all
sudo apt-get install xclip


And then, the test
Code: Select all
echo Hello | xclip -i -selection clipboard
If all works well, you should be pasting the word 'Hello' all over the place.

Now a further test
Code: Select all
cat path/to/some/file.txt | xclip -i -selection clipboard
And now you should have that file's contents ready to paste.

Now make it into a script
Code: Select all
#!/bin/bash
# cat to clipboard
#
cat $1 | xclip -i -selection clipboard
exit
and give it a fancy name like 'clippit'. Then, make it executable
Code: Select all
chmod +x clippit
and copy it into /usr/bin so you can use it like a regular command
Code: Select all
sudo cp ./clippit /usr/bin
by issuing
Code: Select all
clippit /path/to/file
at a terminal.



And there you have it. Happy pasting!
Last edited by optimize me on Wed Aug 12, 2009 3:57 am, edited 1 time in total.
AMD Phenom II X6 1045T 2.8GHz 8GB DDR3 RAM ATI Radeon HD6970
User avatar
optimize me
Level 5
Level 5
 
Posts: 639
Joined: Wed Jun 03, 2009 5:25 pm

Linux Mint is funded by ads and donations.
 

Re: How-to: Cat a file to your clipboard for pasting.

Postby emorrp1 on Wed Aug 12, 2009 3:53 am

Could I suggest you re-organise your documents so it's not such a long path.
If you have a question that has been answered and solved, then please edit your original post and put a [SOLVED] at the end of your subject header
Hint - use a google search including the search term site:forums.linuxmint.com
emorrp1
Level 8
Level 8
 
Posts: 2322
Joined: Thu Feb 26, 2009 8:58 pm

Re: How-to: Cat a file to your clipboard for pasting.

Postby optimize me on Wed Aug 12, 2009 3:56 am

emorrp1 wrote:Could I suggest you re-organise your documents so it's not such a long path.

You could.

It's not really that deep of a path, but it sure feels like it when I'm typing it out from memory.
AMD Phenom II X6 1045T 2.8GHz 8GB DDR3 RAM ATI Radeon HD6970
User avatar
optimize me
Level 5
Level 5
 
Posts: 639
Joined: Wed Jun 03, 2009 5:25 pm

Re: How-to: Cat a file to your clipboard for pasting.

Postby emorrp1 on Wed Aug 12, 2009 4:19 am

Are you using the shell's filename completion? you only have to type out enough letters to distinctly identify the folder, then press <tab> to complete it, if it doesn't, hitting <tab> twice will show you the possible completions. I only mentioned it because my folder hierarchies only go about 4 folders deep, and I never have a problem quickly changing to them.
If you have a question that has been answered and solved, then please edit your original post and put a [SOLVED] at the end of your subject header
Hint - use a google search including the search term site:forums.linuxmint.com
emorrp1
Level 8
Level 8
 
Posts: 2322
Joined: Thu Feb 26, 2009 8:58 pm

Re: How-to: Cat a file to your clipboard for pasting.

Postby optimize me on Wed Aug 12, 2009 7:41 am

emorrp1 wrote:Are you using the shell's filename completion?


Sometimes, yes, when it occurs to me to do it. But then, still, it's a matter of catting a file (or opening with gedit), manually selecting all the text either with the mouse or CTRL-A, then copying & pasting. This way, it's one command, ready to paste.

I just thought it was a neat trick is all. I'm only now starting to notice some quirks in how it operates, though. I'll have to look more at xclip's options (or maybe another CLI clipboard utility) and see if I can make a better one.
AMD Phenom II X6 1045T 2.8GHz 8GB DDR3 RAM ATI Radeon HD6970
User avatar
optimize me
Level 5
Level 5
 
Posts: 639
Joined: Wed Jun 03, 2009 5:25 pm


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 10 guests