Apple's Technical Note TN2450 describes how to remap keys. It is important to know that Right Command is also Right GUI. Running the following command will switch Right Command and Right Alt (if you also want to do the Left Command and Left Alt, refer to the technical note to get the hex values and the Python code below to do the or
operation).
hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x7000000e7,"HIDKeyboardModifierMappingDst":0x7000000e6},{"HIDKeyboardModifierMappingSrc":0x7000000e6,"HIDKeyboardModifierMappingDst":0x7000000e7}]}'
The table at the bottom of the Technical Note has a list of hex values for each key.
Here is the command to change in the same time the GB to US layout ie.:
- map plus-minus key to tilda