There are times when you need to work with editor and terminal at the same time. By editor I mean separate app (not the terminal based one like Vim or Emacs - for those, shell is CTRL + Z away).

I work with GUI editor from time to time so I wanted to create simple key shortcut to quickly switch from and to terminal without messing with CMD + Tab. After some googling, it turned out it’s doable with small system tinkering involved.

First you need karabiner or karabiner-elements (if you are on Sierra). For karabiner-elements you need to use this fork (at least until this PR gets merged) to use hyper as the modifier key. If you have never heard about idea of hyper key here are some links that will let you familiarize with it.

I already have caps lock key remapped to ctrl so the original ctrl key is left unused which makes it ideal candidate for hyper key.

Let’s start by creating a service which can be remapped to hyper + tab combo later. To do that, open Automator app, select File -> New -> Service then paste the following code snippet:

tell application "System Events"
  set currentApp to name of first application process whose frontmost is true
end tell

if currentApp is "Terminal" then
  set switchToApp to "TextMate"
else if currentApp is "TextMate" then
  set switchToApp to "Terminal"
else
  set switchToApp to ""
end if

if switchToApp is not "" then
  tell application switchToApp to activate
end if

In the example above I use TextMate as my editor and default Terminal app. Feel free to modify this snippet according to your needs. When it’s ready, press CMD + S to save the script, give it some meaningful name (so you can easily find it later).

Now we need to bind our hyper shortcut to the service. open the Preferences -> Keyboard -> Shortcuts, select Services on the sidebar and find the service you previously created (most probably it will be somewhere at the bottom). Tick the checkbox to enable it and assign shortcut to CTRL + tab. Because you already remap left CTRL to hyper you will see CMD + CTRL + Option + Shift + Tab` on the list.

That’s all, now you should be able to switch between terminal and editor with your shortcut of choice.

Happy hacking.

PS. If you know better way to switch between editor and terminal, don’t hesitate to let me know in the comments.