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

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
Post Reply
optimize me

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

Post by optimize me »

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

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

Post by emorrp1 »

Could I suggest you re-organise your documents so it's not such a long path.
optimize me

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

Post by optimize me »

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

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

Post by emorrp1 »

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.
optimize me

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

Post by optimize me »

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.
Post Reply

Return to “Tutorials”