olets / zsh-abbr

The zsh manager for auto-expanding abbreviations, inspired by fish. ~13,000 unique cloners as of May '24, 580+ Homebrew installs 6/23-6/24
https://zsh-abbr.olets.dev
Other
510 stars 18 forks source link

Integration with MacOS global text replacements #35

Closed mortenscheel closed 3 years ago

mortenscheel commented 3 years ago

Hi, thanks for a great plugin!

Yesterday I came across this post on Reddit: Is there a way to get the macOS Text Substitutes feature working in Terminal?

It was a fun challenge, and this is what I ended up with:

while IFS="=" read -r key value; do
    abbr add --session --global --quiet "$key"="$value"        
done < <(defaults read ~/Library/Preferences/.GlobalPreferences.plist NSUserDictionaryReplacementItems | plutil -convert json -o - - | jq -r "map({ (.replace): .with}) | add | to_entries | map(\"\(.key)=\(.value|tostring)\")|.[]")

I have no idea if this is just a fun gimmick, or something a lot of Mac users would like to have. I just thought I'd share it here and let you decide.

Also, I haven't really used plutil or jq before, so there's probably cleaner ways of massaging the plist entries into a usable format.

olets commented 3 years ago

That’s cool, thanks for sharing!

I’m also not an expert in those, but that’s about what I expect jq to look like and I’ve confirmed it works for my system text substitutions.

I’d be happy to include this in the README if you wanted to make a PR. Maybe down by “highlighting” and “vi”?

Instead of read I prefer zsh’s nice syntax for creating an array from multi-line output:

typeset -a my_array=( ${(f)”$(my_cmd)”} )

will read each line of my_cmd’s output into the array my_array. If each item can be more than one line (might be relevant here - I haven’t tested what happens if you have a text substitution where the substitution has line breaks) instead of (f) you can do (ps|thedelimiter|) (can use a different character instead of pipes). Then iterate over that array. You can find some examples in zsh-abbr.zsh - for example in _abbr:import_fish() and _ abbr:import_git_aliases()

olets commented 3 years ago

Closed by PR #37. Thanks!