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.
- x11-utils (if you want seamless copy/paste in terminal window)
If any of above is not installed, you can install them by
sudo apt-get install [packageName]
You will also need Compiz Window Manager, which is installed by default in Ubuntu.
#!/bin/bash action=$1 if [ "$action" != "paste" ] && [ "$action" != "copy" ] then echo "Invalid command" exit 1 fi if [ "$action" == "copy" ] then /usr/bin/xdotool key ctrl+c else /usr/bin/xdotool key ctrl+v fi
Save the script above to the location of your choice, and change its permission to 755. I choose ~/scripts/auto/copy-paste.sh.
chmod 755 ~/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).
#Command line 0 ~/scripts/auto/copy-paste.sh copy #Command line 1 ~/scripts/auto/copy-paste.sh paste
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.
#!/bin/bash wid=$(xdotool getactivewindow) wClass=$(xprop -id $wid WM_CLASS | cut -d\" -f2) action=$1 if [ "$action" != "paste" ] && [ "$action" != "copy" ] then echo "Invalid command" exit 1 fi if [ "$wClass" == "gnome-terminal" ] then if [ "$action" == "copy" ] then /usr/bin/xdotool key ctrl+shift+c else /usr/bin/xdotool key ctrl+shift+v fi else if [ "$action" == "copy" ] then /usr/bin/xdotool key ctrl+c else /usr/bin/xdotool key ctrl+v fi fi
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
#!/bin/bash action=$1 if [ "$action" == "left" ] then /usr/bin/xdotool key alt+left else /usr/bin/xdotool key alt+right fi
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.