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
517 stars 18 forks source link

Using single quotes around abbr value causes error on zsh initialization #10

Closed philwhln closed 4 years ago

philwhln commented 4 years ago

I added the following abbreviation with single quotes. It worked fine in that terminal session.

❯ abbr -a nodes='echo "MASTERS" && kubectl get nodes --selector="kubernetes.io/role=master" && echo "REGULAR NODES" && kubectl get nodes --selector="kubernetes.io/role!=master,dedicated!=heavy" && echo "HEAVY NODES" && kubectl get nodes --selector="kubernetes.io/role!=master,dedicated=heavy"'

When I opened a new terminal I hit this error....

[WARNING]: Console output during zsh initialization detected.

abbr add: Expected one argument, got nodes=echo MASTERS && kubectl get nodes --selector=kubernetes.io/role=master && echo REGULAR NODES && kubectl get nodes --selector=kubernetes.io/role!=master,dedicated!=heavy && echo HEAVY NODES && kubectl get nodes --selector=kubernetes.io/role!=master,dedicated=heavy

Trying to delete it fails...

❯ abbr -e nodes
abbr erase: No matching abbreviation nodes exists
For help run abbr --help
philwhln commented 4 years ago

Looking in ~/.config/zsh/abbreviations, I can see the single quotes have been replaced with double-quotes.

abbr nodes="echo "MASTERS" && kubectl get nodes --selector="kubernetes.io/role=master" && echo "REGULAR NODES" && kubectl get nodes --selector="kubernetes.io/role!=master,dedicated!=heavy" && echo "HEAVY NODES" && kubectl get nodes --selector="kubernetes.io/role!=master,dedicated=heavy""
philwhln commented 4 years ago

Editing this file to escape the quotes fixes the error, so the solution would seem to be doing this escaping of double quotes on persisting the values.

abbr nodes="echo \"MASTERS\" && kubectl get nodes --selector=\"kubernetes.io/role=master\" && echo \"REGULAR NODES\" && kubectl get nodes --selector=\"kubernetes.io/role!=master,dedicated!=heavy\" && echo \"HEAVY NODES\" && kubectl get nodes --selector=\"kubernetes.io/role!=master,dedicated=heavy\""
olets commented 4 years ago

Makes sense, thanks for reporting! Yes, the expansions get saved to the user file wrapped in double quotes. The original quote level should be preserved if possible.

It'll be probably a couple weeks before I'm able to look into it. In the meantime, single quotes will work too if you don't want to gunk up your command with slashes, and the echo's will work without quotes. And it's outside my wheelhouse but in at least some circumstances kubectl's --selector doesn't need quotes.

Cool use case, I hadn't thought to use a single abbreviation for a string of commands. I like it.

olets commented 4 years ago

Fixed in 3.1.2!

If you version control your user abbreviations file you'll probably see some noise after the first time you make a change to any user abbreviations via the command line (ie after the first time abbr updates the user file) — more double quotes, more \s. You don't have to change the way you write expansions, either interactively or editing the user abbrevations file directly — zsh will figure out the quoting / escaping as needed.