Fix the tilde key on Mac with Automator and keyboard shortcuts

· 3 min read

On some non-U.S. Mac keyboards, the tilde key is (mis)placed next to the left Shift button. In this post, I'll show you how to get the tilde where it belongs, next to the “1” key, and how to switch between U.S. and International layouts to easily alternate between keyboards.

Now, there a simple brute-force solution in Eugene Petrenko's post. You can remap keys using the following command in the terminal.

hidutil property --set '{"UserKeyMapping": [{"HIDKeyboardModifierMappingSrc": 0x700000064, "HIDKeyboardModifierMappingDst": 0x700000035}, {"HIDKeyboardModifierMappingSrc": 0x700000035, "HIDKeyboardModifierMappingDst": 0x700000064}]}'

We can just run that commmand to remap the keys but I wanted to take it a step further and also solve the following two problems:

  1. When you restart your Mac, the mapping is reset to standard settings, which means you have to run the command every time you restart.
  2. When you alternate between an International keyboard and a U.S. keyboard, you'll have to change the mapping back and forth. It became a problem for me when I started using an external monitor and an exernal Magic Keyboard, which had the U.S. layout.

Solution

To easily switch between my keyboards, I've set up keyboard shortcuts to remap the tilde.

  • Control-Option-Command-R for the Russian keyboard layout.
  • Control-Option-Command-U for the U.S. layout.

In the following steps, I'll show you how to set this up with the Apple Automator app that comes packaged with MacOS.

1. Create Automator scripts

Apple's Automator software enables automation of repetitive tasks. We will use Automator's ability to execute arbitrary commands in the Terminal (a.k.a. shell scripts) to remap keys.

  1. Open Automator by going to Finder > Applications > Automator or searching for “Automator” using Spotlight.

  2. Click File > New to create a new automation document.

  3. Choose Quick Action and click Choose.

  4. Choose no input in the Workflow receives dropdown.

  5. Find Run Shell Script in the list of actions and drag it to the workflow area.

  6. Paste the following command to the text box. This command remaps the tilde for the International keyboard built into the Mac.

hidutil property --set '{"UserKeyMapping": [{"HIDKeyboardModifierMappingSrc": 0x700000064, "HIDKeyboardModifierMappingDst": 0x700000035}, {"HIDKeyboardModifierMappingSrc": 0x700000035, "HIDKeyboardModifierMappingDst": 0x700000064}]}'

  1. Click the Run button to test if the command is working. You can see the output of the shell script by clicking Results.

  2. Press Command-S to save the workflow and give it a meaningful name, e.g. “Remap tilde for Russian keyboard”.

  3. Repeat all previous steps for the other command that cancels remapping of keys and puts the tilde back to its standard key on the U.S. keyboard.

hidutil property --set '{"UserKeyMapping":[]}'

2. Assign keyboard shortcuts

Custom Automator scripts are called “services” and are shown in the Keyboard shortcut settings. We will set up keyboard shortcuts, so we can easily run those scripts.

  1. Open Keyboard in System Preferences or by typing “Keyboard” into the Spotlight search box.

  2. Choose Services in the left pane

  3. Scroll all the way down in the right pane until you see the new scripts you've just created.

  4. Click Add shortcut and press the key combination you want to assign for each of the services.

3. Test

Now let's test if it works.

  1. Press one of the shortcut keys you created.

  2. You see the gear icon appear in the menu bar for a brief moment.

  1. The keys should be remapped now.

What's next?

I created a git repo in the ~/Library/Services directory and pushed it to a private repository on GitHub. If I ever need to change my laptop, I will be able to quickly restore my settings. I'll save this hack for another post.

References

Tags: macos automator