Search code examples
macosshellnsdictionarynsuserdefaults

Proper backslash definition in a -string (macOS defaults write via shell)


I am trying to set up a macOS keyboard shortcut (ctrl+shift+up_arrow) from the terminal.

After creating it manually from System Preferences I found out that '^$\\U2191' is the string I'm looking for.

So:

defaults write com.hello.app NSUserKeyEquivalents -dict-add 'Test' -string '^$\\U2191'

However, this is what I get by running defaults read com.hello.app NSUserKeyEquivalents:

{"Test" = "^$\\\\\\\\U2191";}

Why every backslash is being duplicated twice? How to properly input only two backslashes?


Solution

  • It turns out that, when it comes to special characters, the only way to edit the plist file from the cli is to include the special characters themselves.

    defaults write com.hello.app NSUserKeyEquivalents -dict-add 'Test' -string '^$↓'
    

    I don't know how other keys are dealt with, but PLIST Editor is a good way to quickly grab those values after manually creating the shortcut the first time.