Subscribe:   RSS icon   twitter icon

You Need a Middle Mouse Button

(And you don’t have one)

Lee Phillips
February 28th, 2022

Read this article if: you use Linux with X11 and no desktop environment; you need to have a middle mouse button; there is no such button on your input devices.

This may be of use to you even if you use a desktop environment, but I wouldn’t know: I haven’t used one of those monstrosities in years.

I also got along fine for years without a middle mouse button. What pushed me over the edge was sioyek, a wonderful PDF reader designed especially to make reading academic-type stuff, containing footnotes, endnotes, and references to figures, easier. Sioyek uses the middle-click for a critical function. Its keyboard commands are all reconfigurable, but not its mouse clicks. My keyboard has an integral trackpad with only two buttons, used for left and right. I can use a two-finger tap to emulate the right button, but was not able to configure a tapping gesture to emulate a middle button. So I had to figure out how to use one of my otherwise unused keys to stand in for the middle button.

In Linux you can use a middle-click in the terminal to copy and paste the selection in one go, which is a nice bonus. Also, in many graphical programs, such as web browsers, the middle mouse button, when clicked while the pointer is in the scrollbar region, snaps the page to the scroll position of the pointer, rather than moving by one screenful.

The first step is to decide which key you want to use for the middle mouse button. I happen to have one centered near the top of the trackpad, that I don’t use for anything, so this was an easy choice. You then need to discover the keycode for this key. Use the xev command, which will tell you the keycode for any key you press (and an enthusiastic stream of other information you don’t need for this). The keycode for my chosen key was 166.

Step two is to add a definition to your .Xmodmap file, or whatever file you use on startup to configure xmodmap. Of course you already have one, to map your CAPSLOCK key to something more useful, and make other adjustments. Here is the line to add:

keycode 166 = Pointer_Button2

Of course, you will use your desired keycode instead of “166”.

To make this work there is a step three: if you don’t already have the xkbset command installed, install it. I use Debian, and it’s in the package manager.

Add these two lines to your .xinitrc file, or some other configuration file you’ve set up that gets invoked when X11 starts:

xkbset m  # activates the mouse-button key equivalents 
xkbset exp =m # turns off expiration of above

This command is intended mainly for activating accessibility features, such as sticky keys, so it’s convenient to have them optionally expire after a while. But you want your emulated middle mouse button to work forever, so you need to turn the expiration off.

That’s all there is to it. You can turn this on without rebooting or even restarting X11. The xkbset commands take effect immediately, and you can invoke xmodmap manually for instant remapping.

Please let me know if you have any additions to the page that might be helpful, and happy middle-button-mousing.

Share with Facebook Share with Twitter Share with Reddit Share with Slashdot
▶ Comment is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to
Quotilizer ... loading ...

Tenuously related:

The best tool for CVs with publication lists.