135

In Windows I used command-line clipboard copy-and-paste utilities...

  • pclip.exe and gclip.exe

These were UnixUtils ports for Windows (but they only handled plain text). There were a couple of other native Windows utilities which could write/extract any format.

I've looked for something similar in Synaptic Package Manager, but I can't find anything.

Is there something there, that I've missed? ... or maybe this is available in Bash scripting?

The type of utility I'd like will be able to read/write via std-in/std-out or file-in/file-out, and handle Unicode, Rich Text Format, picture, etc. clipboard formats...

NB: I'm not after a clipboard manager.

  • See related question from unix.SE: unix.stackexchange.com/questions/30093/… – landroni Apr 2 '14 at 7:49
  • vim[enter]"+P :3 (run vim and then from " register - i.e. system clipboard - paste text). Then you have (one of) the best text editor(s) at your finger tips, so you can :w filename. Also, I always run vim in tmux which means that if I need to copy a little text over to the terminal, I just select (with v or V for line selection) and then "+y (which yanks or copies to clipboard), then I make a new tmux pane and can <ctrl>+<shift>+v to paste. Additionally, I will occaisionally use xsel -b > filename if I need to write clipboard to a file. Cheers ^u^ – dylnmc Jun 2 '16 at 20:47

14 Answers 14

134

xsel

I am using xsel Install xsel, but I am only using it with plain text and unicode text.

xsel can copy and paste to three different "clipboards".

By default, it uses the X Window System primary selection, which is basically whatever is currently in selection. The X Window System also has a secondary selection (which isn't used much), and a clipboard selection. You're probably looking for the clipboard selection, since that's what the desktop environment (e.g. Gnome, KDE, XFCE) uses for its clipboard. To use that with xsel:

xsel --clipboard < new-clipboard-contents.txt
xsel --clipboard > current-clipboard-contents.txt
| improve this answer | |
  • 1
    Adi... xsel seems(?) to be only text based (as you mentioned), so I'll look for a more comprehensive utility... However, it has been very interesting for me to read its info documentation... I knew that middle-clicking the mouse (in the Terminal) would paste the currently selected text into the command-line, but I didn't realize that this feature applies to most X apps... The most-recent text selection can be pasted via a middle-click (but some apps seem to override it).. Two clipboards!.. Very handy.. .I must read more about the 3rd X-selection buffer. – Peter.O Nov 7 '10 at 15:14
  • xsel does work with Unicode text. You can read more about X clipboards on the freedesktop wiki pages: freedesktop.org/wiki/Specifications/ClipboardsWiki . – Adi Roiban Nov 7 '10 at 16:27
  • I've got to get my head around this new info.. 3 clipboards! PRIMARY, SECONDARY, CLIPBOARD... This is very different to what I'm used to in windows... The 3rd one "CLIPBOARD" seems to be the same as a Windows / Mac clipboard... but it is ridiculously late here.. I'll get back to this tomorrow... (Inersting, but too much to read now..) – Peter.O Nov 7 '10 at 16:54
  • Thanks... xsel does the job (for text)... Something might turn up for other formats. – Peter.O Nov 9 '10 at 20:17
  • 8
    xsel -b is short for xsel --clipboard. – Andrei Duma Sep 28 '15 at 14:26
93

xclip

Install xclip may be one of your choices.

cat samples.sh | xclip -sel clip

you can also setting it in the .bashrc file using alias.

alias clipboard='xclip -sel clip'

The key to paste binary data to a file with xclip is to tell what Media Types you have on clipboard. For PNG you can:

xclip -selection clipboard -t image/png -o > "`date '+%Y-%m-%d_%T'`.png"

So now on ~/Dropbox/.mybashrc ;) you can add an alias (clipboard2photo) to easily paste to image file (maybe someday we'll have it on Nautilus).

| improve this answer | |
  • This looks good, but I just haven't had a chance to look into it, yet... I will do that ASAP... – Peter.O Nov 8 '10 at 7:03
  • xclip also only handles text selections. – Steve Beattie Nov 8 '10 at 22:04
  • 2
    Also, the command can be as short as xclip -se c – Jeromy Anglim Oct 5 '11 at 1:05
  • 1
    I don't understand why this is not in the man page – Sergiy Kolodyazhnyy Apr 24 '15 at 6:37
  • 1
    I get this when trying to paste images to file Error: target image/png not available – Emilio Nicolás Jul 21 '16 at 12:08
12

Parcellite

I've used parcellite for many years as a clipboard manager. It runs in the Notification Area and offers many options for managing both Primary Selection and standard freedesktop.org Clipboard.

Tray Icon

It's been my answer to managing clipboard items.

Preferences

| improve this answer | |
  • This looks like a good panel-app.. The "Actions" is handy... but unfortunately its not the type of tool I am looking for.. I've re-worded my question to include some more specific info... Maybe the exact thing I'm after is an obscure Python script, or the like... There were a couple of very good native Windows-only utilities available, so I'd think an equivalent would be floating around somewhere in the Ubuntu/Linux world... – Peter.O Nov 7 '10 at 16:18
  • FYI, Parcellite has a cool feature - history of copied text. To activate auto-paste from history dialog, you should also install xdotool – Noam Manos Jan 25 '18 at 14:01
11

I recently came across the tools xclip-copyfile, xclip-cutfile, and xclip-pastefile (package xclip). These tools allow you to copy/cut/paste entire files via the command line.

You can see their man page here.

| improve this answer | |
10

clipit ( http://clipit.rspwn.com/ ) is a clipboard manager for gtk that allows piping text from the commandline:

ls | clipit;

Edit: Currently, there is nothing on ClipIt's official website but you can get it from GitHub

| improve this answer | |
  • I am using ClipIt for other reasons and didn't know it's capable of getting data from terminal. Thanks a lot for that info. – Juraj.Lorinc Dec 23 '17 at 13:33
4

You can use CopyQ for this. From the website:

Clipboard manager with advanced features

  • CopyQ is clipboard manager with searchable and editable history.
  • Supports Linux and Windows.
  • Experimental support for OS X 10.9+.
  • Store text, HTML, images and any other custom format.
  • Advanced command-line interface and scripting.

To copy a text file:

copyq action "cat /tmp/file.txt" "" && copyq select 0

To copy an image (you need to indicate the MIME type):

copyq write image/png - < file.png && copyq select 0

To copy a data file (you need to indicate the MIME type):

copyq write application/pdf - < file.pdf && copyq select 0

Check their wiki for more documentation and usage examples.

You can install it either via the ppa:noobslab/indicators PPA or by manually downloading prepackaged .deb files of latest stable from the project's Downloads section.

| improve this answer | |
4

If you just want to copy some text to the clipboard:

echo -n "some-text" | xclip -selection clipboard

| improve this answer | |
2

Pastie

A new clipboard manager with Indicator Applet support was recently released (though not in the archive yet). In addition to tracking text clipboard history it will also capture and persist data clipboard entries as well (Files, Folders, etc) which may be useful if you manipulate files a lot via a GUI/Clipboard environment.

alt text

You can add the following PPA: ppa:hel-sheep/pastie

| improve this answer | |
  • Thanks for the suggestion Marco, but I don't have any need for a clipboard manager. Occasionally I'll paste some temporary text into a text file , but I actually find that a "history manager" slows things down (but that's just me).. I'm really after a command-line tool which can selectively extract/store a specific format from/to the clipboad.. I am assuming the Ubuntu clipboard is similar to the Windows one (I'd be stunned if it was significantly different, because users' requirements are the same on any platform)... I'm sure someone will find your suggestion useful. – Peter.O Nov 7 '10 at 15:45
1

I think the following tool will satisfy your requirements:

Cut and paste files in terminal

| improve this answer | |
  • Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. – guntbert Nov 23 '13 at 15:50
1

How about GPaste daemon settings, a clipboard management daemon applet with DBus interface:

  • GPaste is a clipboard management suite for GNOME written in Vala
  • GPaste settings look has been updated to better fit with recent GNOME
  • Daemon now fully evenmential (no more polling, less power consuption)
  • GPasteClipboard now has a “owner-change” signal similar to GtkClipboard’s one
  • New setting to set the maximum amount of memory used by data in history
  • FIFO mode has been dropped (lacking design)
  • Code cleanup and modernisation
  • g_paste_history_add is now transfer full
  • g_paste_keybinder_add_keybinding is now transfer full

Installation:

To install it in Ubuntu 13.10, 13.04, 12.10 or 12.04, use the commands below:

sudo add-apt-repository ppa:webupd8team/gnome3
sudo apt-get update
sudo apt-get install gnome-shell-extensions-gpaste

Or install gpaste through synaptic package manager (and also gnome-shell-extensions-gpaste if you use Gnome-Shell).

Another option is the installation through software-center.

enter image description here

| improve this answer | |
1

I'm trying to develop a better solution that isn't a clipboard manager, found at https://github.com/baitisj/copypasta . It doesn't handle RTF and HTML correctly quite yet, and large images pose problems, but feel free to fork and submit patches.

| improve this answer | |
1

For RTF format I use this procedure: I copy text into clipboard in the application. If I am unsure whether it is rtf or what, I use this command to verify:

xclip -selection clipboard -o -t TARGETS

When it prints out something like this:

TARGETS

text/rtf

text/richtext

So I cannot resist to exclaim quite aloud: "Gee, that is genious! God bless linux!" After that shot prayer, just obvious thing follows:

xclip  -selection clipboard -o -t text/rtf > mypreciousRTFcontent.rtf

Then I process it all in libreoffice and send to all my friends as a spam.

:-)

| improve this answer | |
-1

Yes, there is. Try tmux which is more advanced than screen. To install tmux, type this in the terminal:

sudo apt-get install tmux

Also try gpm. The commands gpm and tmux might be a good combination...

| improve this answer | |
  • 2
    Screen is a multiple-terminal emulator not a clipboard manager. – Marco Ceppi Nov 7 '10 at 14:25
  • It seems that screen and tmux only work at the terminal level (I can't find any reference to "clipboard" in either of their info documentation)... What I'm looking for is a system-wide clipboard copy and paste utility... I probably should not have used terminal as a tag... The type of utility I want will be able to read/write via std-in/std-out or file-in/file-out, and handle Unicode/Rich-text/Picture/etc clipboard formats... – Peter.O Nov 7 '10 at 14:30
  • I've just found a reference on stackoverflow... gpm is for a non-X terminal "clipboard", and GNU screen has its own "clipboard" also... I see now why you mentioned them... This is great info, and it has led me into a deeper understanding of what goes on behind the scenes... I'll stick to the basic Xterm "selections system" (clipboad) for now.. The Linux "clipboard" seems to be so different to Windows, that what I was looking for may be done in a very different way here.. +1 :) – Peter.O Nov 9 '10 at 19:54
-1

Yo ya gnomers, that's what ya get for using CORBA-crap.
There's nothing better for yar software than using tools not installed by default.

Had ya used a proper desktop-environment, such as KDE-plasma, you would not have that problem, because ya could just use dbus, from whatever software ya like, e.g. the console:

qdbus org.kde.klipper /klipper setClipboardContents "NiHao"
qdbus org.kde.klipper /klipper getClipboardContents

Personally, I actually connect to dbus directly with C#, which means I don't need qdbus.
That's probably of no use for ya, since ya don't have klipper, but I thought I'd mention it anyway, just to annoy ya with how it's done properly.

PS: Don't copy that C# code, Task.Result might deadlock.

private static async System.Threading.Tasks.Task<string> GetClipboardContentsAsync()
{
    string clipboardContents = null;

    Tmds.DBus.ObjectPath objectPath = new Tmds.DBus.ObjectPath("/klipper");
    string service = "org.kde.klipper";

    using (Tmds.DBus.Connection connection = new Tmds.DBus.Connection(Tmds.DBus.Address.Session))
    {
        await connection.ConnectAsync();

        IKlipper klipper = connection.CreateProxy<IKlipper>(service, objectPath);

        clipboardContents = await klipper.getClipboardContentsAsync();
    } // End Using connection 

    return clipboardContents;
}


private static string GetClipboardContents()
{
    return GetClipboardContentsAsync().Result;
}


private static async System.Threading.Tasks.Task SetClipboardContentsAsync(string text)
{
    Tmds.DBus.ObjectPath objectPath = new Tmds.DBus.ObjectPath("/klipper");
    string service = "org.kde.klipper";

    using (Tmds.DBus.Connection connection = new Tmds.DBus.Connection(Tmds.DBus.Address.Session))
    {
        await connection.ConnectAsync();

        IKlipper klipper = connection.CreateProxy<IKlipper>(service, objectPath);

        await klipper.setClipboardContentsAsync(text);
    } // End using connection 

}


private static void SetClipboardContents(string text)
{
    SetClipboardContentsAsync(text).Wait();
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.