What is the problem?

In my I have an external monitor, external keyboard and of course - an external mouse. This mouse has 2 additional buttons, the so called - button 4 and button 5. Usually (maybe in Windows?) they are used to go back/forward (e.g. in a browser) and this works pretty nicely. Unfortunately in MacOSX this is not supported by the OS and you need additional software to achieve the similar functionality that comes in Windows out of the box.

Solutions

Use 3rd party software for additional mouse button

Of course, the easiest thing is to install a 3rd party software that just takes care of that. You have SensibleSideButton (free), SteerMouse (paid), USB Overdrive and probably many more…

Now, I haven’t tried all of them because some of them are paid, however I’ve tried the SensibleSideButton and it works pretty good - that’s if you want to have go back/forward functionality even if the window is not focused (you can read more info on the app’s website, the author tells a pretty compelling story). Long story short, it works great if the app knows that you are doing a 3-finger swipe, but VS Code doesn’t support this (more info: GitHub). Being a developer, actually using the mouse 4/5 buttons to navigate in VS Code sounded to me as pretty good excuse to spend a couple of hours to find a workaround that.

…or just use Karabiner

Now, Karabiner is a great piece of software, you can add keyboard shortcuts based on different conditions (you can check all the options here). But little did I know that you can control your mouse with Karabiner, by default this is turned off, but if you want to enable it, you should open Karabiner’s settings go to Devices and select your mouse. Once you’ve done that - you can now do some mouse shortcuts! :)

The approach I have is pretty simple - for VS Code send the appropriate shortcuts (Ctrl + - and Ctrl + Shift + -), for everything else send Cmd + Left or Cmd + Right. For most applications this works as expected as these are the MacOSX prescribed shortcuts. To be honest, I care for this only for Google Chrome, but you can easily tune it for a specific app in case it doesn’t follow the commonly accepted shortcuts in MacOSX.

Click here for the source code

Click here to add to Karabiner

Conclusion

I always try to solve a proble with the tools I have, sometimes it may be impossible, but sometimes you can do it! The benefit of using the same software is you don’t have to install yet another app and to study its quirks!