In Greece we are using the US keyboard layout and that means that the tilde key (~) is placed next to number 1 at the top left corner of the key, like the following picture:

US English Mac keyboard layout

About a year ago we moved to Luxembourg and my old Macbook died, so I had to replace it with a new one that unfortunately has the German layout:

German Mac keyboard layout

The problem is that even if you set the keyboard layout to US in MacOS when you hit the key next to 1, this symbol starts appearing §. I don’t have anything with the symbol but in my day to day it’s totally useless and I started missing my beloved symbol (~) which is so often used in coding.

Long story short, I had to find a way to swap places, through software, between the key, right next to left Shift key, and the one right next to 1. To do so, I experimented with different apps, scripts, etc. but what worked at the end is the following.

Swap tilde ~ key with §

Open your favourite terminal Terminal, iTerm, etc.. and run the following script:

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

Now if you press the key next to 1 it will show up as ` and the key next to shift as §. This solution works until next restart.

If you want to make it permanent you need to run the following scripts:

Step 1. Store the following script in your home directory

cat << 'EOF' > ~/.tilde-switch && chmod +x ~/.tilde-switch
hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000035,"HIDKeyboardModifierMappingDst":0x700000064},{"HIDKeyboardModifierMappingSrc":0x700000064,"HIDKeyboardModifierMappingDst":0x700000035}]}'
EOF

You can check your script at ~/.tilde-switch file your home directory.

Step 2. Create a task to run the file from step 1 on every startup

Run the following command, it will require admin privileges:

sudo /usr/bin/env bash -c "cat > /Library/LaunchAgents/org.custom.tilde-switch.plist" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>org.custom.tilde-switch</string>
    <key>Program</key>
    <string>${HOME}/.tilde-switch</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <false/>
  </dict>
</plist>
EOF

Step 3. Activate the task from step 2

Run the following command:

sudo launchctl load -w -- /Library/LaunchAgents/org.custom.tilde-switch.plist

Undo the key swap

If you want to go back to the previous key setup, before the swap, you can do the following:

Undo the only switching script

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

Undo all three steps

sudo launchctl unload -w -- /Library/LaunchAgents/org.custom.tilde-switch.plist; sudo rm -f -- /Library/LaunchAgents/org.custom.tilde-switch.plist ~/.tilde-switch

 

Quick Update for MacOS 14.2+

After the update to MacOS 14.2, it seems that the above commands do not work and the only way I found that this works is by using Karabiner-Elements and just adding the following entry in Simple ModificationFor all devices – `non_us_backslash` -> `grave_accent_and_titlde`.

Categorized in: