When did you first run into a computer with a mouse? How many buttons did it have? Most UNIX-like operating systems provide two separate copy buffers; the first provides the standard copy and past functionality everyone knows and loves (Ctrl + C, Ctrl +V), while the second allows you to paste selected text directly. The use of this secondary buffer has been dependent largely on mouse design for the last few decades, and as the hardware has changed, so has the usage of this copy buffer.
First, A Little History
I was familiar with the original Mac mouses with their curious single button, but the first mouse-based computer I used was a Sun workstation in the early 90’s while I was in college. It had THREE buttons; a proper mouse! The left button selected things, the right brought up context menus, and the middle button pasted from the secondary copy buffer. Unfortunately, UNIX workstations never really caught on with the public.
On Windows, where there was only a single copy buffer making the middle mouse button was unnecessary and leading to the two button mice becoming the norm. When Linux emerged on the scene, it ran on hardware designed for Windows since that was what was generally available. Like the powerful UNIX workstations it used X11 as its graphical desktop environment, and thus came equipped with the two copy buffers and middle button paste functionality. However, with the mouse hardware of the day being the two-button variety, this functionality was not available to most users.
Somewhere along the way, someone came up with the idea of simulating the middle mouse click by simultaneously clicking both the left and right buttons. For desktops with discrete mice, that worked well enough. Eventually, three-button mice made a comeback via the scroll-wheel mouse, and all was good.
Laptops have been a bit of a different story. Different manufacturers design things differently: A few manufacturers nicely include three-button functionality but many do not. If the laptop has two distinct buttons, the middle-mouse emulation tends to work acceptably.
A trend that’s ongoing is a minimalist design aesthetic that uses a clickpad. Clickpads don’t have physically distinct buttons at all, but rather a clickable surface, that can register presses on corners as button clicks. Unfortunately, many clickpads seem unable to easily register simultaneous left and right clicks making middle-mouse paste unreliable and difficult as a result.
Adapting Middle-Mouse Paste to New Hardware
To work around this problem it’s possible to map the functionality to a hotkey combo on the keyboard. This can be done with xdotool, a command that can create mouse events synthetically. There are a few other similar tools out there but this one worked best for me.
Xdotool is a command line utility to simulate keyboard and mouse activity that uses X11’s XTEST extension. Its functionality includes moving, resizing, hiding, and modifying windows, switching desktops, and so on.
Here’s the script to simulate the middle mouse click; save this as ‘middle-paste’:
#!/bin/bash xdotool mousedown --clearmodifiers 2 xdotool mouseup 2 xdotool keyup alt xdotool keyup ctrl xdotool keyup shift
The first two lines with mousedown and mouseup on button 2 does what you would expect it to do: Button 2 is the middle mouse button, and this script presses and then releases it. It clears modifiers (the control, shift, and alt keys) before the mouse click simulation so it can map to a hotkey combo like v and drop the Ctrl and Alt modifiers before the middle mouse click is communicated. Otherwise the client application will receive , which is unlikely to do what we want. Since we are clearing the modifiers, we also need to force X to think the keys have been released via the three keyup commands; otherwise the keyboard will be left in a weird state.
Save this script to a file named ‘middle-paste’ and put the file somewhere in your path, such as ‘~/bin’. You’ll also need to set the execute bit:
$ chmod a+x ~/bin/middle-paste
With that, let’s test it out. With your mouse, highlight some text, such as this sentence.
Next, in your terminal run:
This should print out whatever text you selected. If it does, you’re ready to map this to a hotkey. In Ubuntu:
0. System Tools > Preferences > Keyboard
1. Go to the Shortcuts tab
2. Custom Shortcuts
3. Click ‘+’ to add a shortcut. Add:
Name: middle-paste Command: /middle-paste
4. Set the key combo for the shortcut to Ctrl+Alt+V
For different desktops or distros, the steps will be different but the concept the same.
Some Final Notes
One idiosyncrasy I’ve noticed is that when using this to paste into Firefox, ‘> ‘ always gets prepended to the text. This becomes an annoyance when pasting URLs into the URL bar, since Firefox then doesn’t recognize the URL as a URL! Remember to hit the Home key and a couple Del’s to remove the greater-than sign and the extraneous space. I suppose Firefox adds this as a safety feature on accidental pastes, but the exact reasoning I could only guess at; I’d certainly like to know how to disable this behavior!
Depending on your hardware, you may be able to achieve a middle mouse emulation some other way. Some laptops provide a . While other times, the touchpad driver has an option to emulate middle click or a way to reconfigure the pad to recognize a middle click zone. On systems able to recognize gestures, you might map middle mouse click to a gesture like three-finger-tap.
Who Cares About Copy Buffers Anyways?
At this point, you might be like my wife asking why not just use Ctrl+c and Ctrl+v like a normal person?
First, some Linux applications map Ctrl+c and Ctrl+v to other functionality. Emacs is known to do this, but so are many other terminal-based applications. To cut and paste in these applications with the first copy buffer, you’d be required to use the menus, but with the second copy buffer you can just select the text at the source, and then middleclick (or Ctrl+Alt+v) where it is supposed to go.
Second, on laptops with clickpads sometimes it’s hard to right-click on a text selection to access the context menu’s copy command without accidentally deselecting the text. Using the second copy buffer simplifies this operation: select the text, then type ++v to paste
Who knows what strange input devices we’ll see in our future; by familiarizing yourself with the powerful xdotool utility you’ll be able to work around inevitable missing features like middle-mouse paste to make more efficient use of your computing environment.