Different ways to add a replacement:
defaults write -g NSUserDictionaryReplacementItems -array-add '{on=1;replace=a;with=b;}'
defaults write -g NSUserDictionaryReplacementItems -array-add '<dict><key>on</key><integer>1</integer><key>replace</key><string>a</string><key>with</key><string>b</string></dict>'
Different ways to replace the whole list:
defaults write -g NSUserDictionaryReplacementItems -array '{on=1;replace=a;with="b \"\\b";}' '{on=1;replace=c;with=d;}'
defaults write -g NSUserDictionaryReplacementItems '({on=1;replace=a;with="b \"\\b";},{on=1;replace=c;with=d;})'
defaults write -g NSUserDictionaryReplacementItems '<array><dict><key>on</key><integer>1</integer><key>replace</key><string>a</string><key>with</key><string>b "\b</string></dict><dict><key>on</key><integer>1</integer><key>replace</key><string>c</string><key>with</key><string>d</string></dict></array>'
Quit and reopen applications to apply changes.
Edit: the changes made with defaults
are lost if you open the Text tab of the Keyboard preference pane. To prevent it, use a script like this:
date=$(date +%s)
while read -r replace with; do
plist+="{on=1;replace=\"$replace\";with=\"$with\";},"
sql+="INSERT INTO 'ZUSERDICTIONARYENTRY' VALUES($((++i)),1,1,0,0,0,0,$date,NULL,NULL,NULL,NULL,NULL,\"$with\",\"$replace\",NULL);"
done < <(sed 's/\\/\\\\/g;s/"/\\"/g' ~/replacements.txt)
sqlite3 ~/Library/Dictionaries/CoreDataUbiquitySupport/$USER~*/UserDictionary/local/store/UserDictionary.db "delete from ZUSERDICTIONARYENTRY;$sql"
defaults write -g NSUserDictionaryReplacementItems "(${plist%?})"
The replacements are saved to ~/Library/Dictionaries/CoreDataUbiquitySupport/$USER~*/UserDictionary/local/store/UserDictionary.db
even if iCloud is disabled.
defaults
for lots of things, but writing to arrays and dictionaries is something I’ve always avoided.defaults read -globalDomain <key>
as an alternative toNSGlobalDomain
, if you wish. I find it easier to remember.)