Use Mouse Buttons to Perform Copy / Paste in Ubuntu

Updated (3/4/2013): If you don't have Compiz, you may read this article first.

As a programmer, the ability perform copy and paste efficiently is vital to me. What can be more efficient than Ctrl+C and Ctrl+V -- you might ask. Well, they are very efficient if you are right-handed, but I am left-handed (or rather, ambidextrous). I prefer to use the mouse with my left hand.

Here's the problem: when I am using a mouse, how can I do copy and paste without my left hand leaving the mouse for the keyboard? The right-click-to-bring-up-the-context-menu-and-copy thing will not do -- in order to copy like that, I need to first right click to bring up the context menu, move the mouse cursor the "Copy" position in the context menu, click on it; to paste, I need to right click again to bring up the context menu, and move the mouse cursor to the "Paste" position, click on it. That's four clicks and two movements. I am not all satisfied. After some experiment, I come up with a little script that will allow me to perform copy and paste quickly on a mouse.

Prerequisites

  • compizconfig-settings-manager
  • xdotool
  • x11-utils (if you want seamless copy/paste in terminal window)

If any of above is not installed, you can install them by

You will also need Compiz Window Manager, which is installed by default in Ubuntu.

The Script

Save the script above to the location of your choice, and change its permission to 755. I choose ~/scripts/auto/copy-paste.sh.

Invoke the Script

I am going to bind the copy and pate commands to mouse button 6 (backward button) and mouse button 7 (forward button) respectively. These button are broken in Ubuntu, and I don't intend to fix them. So these are free buttons to me.

Open "CompizConfig Settings Manager," and then go to "General", and then "Commands" tab. Enter the following commands into Command line 0 and Command line 1 (Use other slots if any of the slot is occupied).

Once you have entered the script paths in Command line 0 and Command line 1, go to "Button Bindings" tab, and bind each command to the desired mouse button. In my case, I used button 6 and button 7. To find out the button number on your mouse, try the following command

Close CompizConfig Settings Manager when you are done.

Once you have reached here, you will be able to perform copy and paste using the mouse button you assigned earlier.

Seamless Copy/Paste in Gnome Terminal and Regular Text Selection

In Gnome terminal, the shortcut for copy is Ctrl+Shift+C, and paste Ctrl+Shift+V. Is there a way to make copy/paste function work seamlessly across regular text window and Gnome terminal window? The answer is yes.

Other Thoughts

Though the backward/forward mouse button are broken in browser, they can be fixed easily using the aforementioned method.

Restore Mouse Backward/Forward Button Functionality

As you can see, combining xdotool, xprop, your imagination, the ability to bind a command to a mouse button, you can add some really useful functions to the extra mouse buttons. As always, you are welcome to leave feedback, or perhaps share the script you created below.

14 Comments Use Mouse Buttons to Perform Copy / Paste in Ubuntu

  1. Dana Raquel Bryant

    Thanks a bunch for sharing this with all people you really understand what you are speaking about! Bookmarked. Please additionally discuss with my site =). We could have a hyperlink alternate arrangement among us!

  2. Neil Smithline

    The entire clipboard/selection mechanism of X is antiquated. It dates back to a time when extensibility and customizability were valued over ease-of-use.

    That was fine 15 years ago but the consistency found in Windows, Mac OS X, etc... have shown that, with the appropriate trade-offs, ease-of-use is possible out of the box without a significant loss of flexibility.

    While your solution is disgusting, it is, by far, the most comprehensive solution that I have seen. While having to add a conditional for every window that has a different copy/paste key mappings is a major pain, it is something that can be done due to your clever use of xprops.

    All of the other solutions to this problem that I have found require you to tweak settings in each app that. IMO, there is no doubt that having all of the config in a single script is better.

    TY for sharing!

  3. Peter de Lacey

    Why not make use of the built-in (in Linux) method:
    Highlight text to be copied (this automatically copies it), then move the mouse cursor where you want it pasted and click the middle mousebutton. One click is all it takes. I use this all the time. I can't try your method because unfortunately compiz won't run on my Mint 14 KDE desktop (don't know why).

  4. Pingback: Use Mouse Buttons to Perform Copy / Paste in Linux (Updated) : Code Central

  5. Cuong

    @Peter

    Didn't know this method, thanks for pointing it out. Seems to be working well initially, but there are some edge cases that the copy buffer got overwritten. Also, I can't do the following

    select some text
    copy
    select another text region
    paste

  6. Sam

    Thanks for the post! However, installing the compiz configurator on Mint 16, there appears to be no config tab under general tab. Trying to create a Profile (which may allow new commands) is apparently buggy, as no message, but no new profiles either.

    So....I hope my tool will use c&p keys. It is TOTALLY amazing to me, that in this day in age, after almost 20 years of | plus z,x,c,v have ALWAYS been Undo, Cut, Copy, and Paste...in virtually EVERY OS I've EVER used... in my 30+ years as a programmer.

    ...oh EXCEPT in the BRAND NEW Mint 16 Ubuntu flavor!!! (which is supposedly a great dev env...well maybe not yet!)

  7. Bryan Larsen

    For those who aren't using compiz or are looking for a more general solution, you can use xbindkeys. For example, here's a snippet of my .xbindkeysrc:

    "~/bin/copy.sh"
    b:9 + release

    "~/bin/paste.sh"
    b:8 + release

  8. Pingback: Terminal Copy Without Mouse | Nitanitaa

  9. car insurance

    Que no seas mal pensado Rafa, que las siglas de su unidad son SS y que no tienen nada que ver con las otras SS.¡Angelitos!Salud, República y Socialismo

  10. http://www.envyoftheworld.com/

    Joo, Ihmiskokeet on helppoja ja eettisiä by amateurs kun puhutaan korvanapeista tai magn.rannekkeista tai homeovedestä. Ihan toinen asia on oikeat sairaudet ja oikeat reseptilääkkeet ja oikeat potilastilastot. Kokeileva ja potilastietoja kräkkäävä amateur istuu äkkiä linnassa myyjän sijasta.Jos amateur leikkisi freelance-journalistia ja suhteilla poimisi jonkun jymy-skuupin+paljon tuuria ja taitoa internetissä + tuuria extralisää niin sitten olisi toivoa.. ?

Leave a Reply

Your email address will not be published. Required fields are marked *