Tilde (~) and plus-minus (±) in wrong place on keyboard I'm using a MacBook Pro with a Swedish physical keyboard, but with U.S. layout defined in System Prefs. However the tilde (~) and plus-minus (±) keys seem switched. The following key combination should produce a ~:



Because the mapping in System Prefs says so:



But it results in a ± symbol.

It seems these two keys (± and ~) are switched somehow. How do I make them work as shown in System Prefs?
Here’s a solution that does not use any external tools.
Run this in Terminal.app:
hidutil property --set '{&quot;UserKeyMapping&quot;:[{&quot;HIDKeyboardModifierMappingSrc&quot;:0x700000035,&quot;HIDKeyboardModifierMappingDst&quot;:0x700000064},{&quot;HIDKeyboardModifierMappingSrc&quot;:0x700000064,&quot;HIDKeyboardModifierMappingDst&quot;:0x700000035}]}'

Now try your ~ and ± keys: they should be switched around.
Problem is, this fix will only work until the next restart. To make it permanent, you have to automatically run it on system load.
You can do that in three Terminal.app commands:


cat &lt;&lt; 'EOF' &gt; ~/.tilde-switch &amp;&amp; chmod +x ~/.tilde-switch
hidutil property --set '{&quot;UserKeyMapping&quot;:[{&quot;HIDKeyboardModifierMappingSrc&quot;:0x700000035,&quot;HIDKeyboardModifierMappingDst&quot;:0x700000064},{&quot;HIDKeyboardModifierMappingSrc&quot;:0x700000064,&quot;HIDKeyboardModifierMappingDst&quot;:0x700000035}]}'
EOF

(This is all a single command.) This stores the script from above in an invisible executable file in your home directory, ~/.tilde-switch.


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

(This is all a single command.) This creates a system task: ‘run the file from step 1 on every startup.’


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

This loads (activates) the task from step 2.


Steps 2 and 3 will prompt for your password. And now, your ~ and ± keys are permanently switched.
Note 1
This appears to work not only on the MacBook's build-in internal physical keyboard, but on the external keyboards as well.
Note 2
To undo the switching script (not the three steps), here’s the reverse script:
hidutil property --set '{&quot;UserKeyMapping&quot;:[{&quot;HIDKeyboardModifierMappingSrc&quot;:0x700000035,&quot;HIDKeyboardModifierMappingDst&quot;:0x700000035},{&quot;HIDKeyboardModifierMappingSrc&quot;:0x700000064,&quot;HIDKeyboardModifierMappingDst&quot;:0x700000064}]}'

Note 3
To undo the three steps:
sudo launchctl unload -w -- /Library/LaunchDaemons/org.custom.tilde-switch.plist; sudo rm -f -- /Library/LaunchDaemons/org.custom.tilde-switch.plist ~/.tilde-switch

Credit
This solution is inspired by this article:
http://homeowmorphism.com/articles/17/Remap-CapsLock-Backspace-Sierra