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

Wanted: Placeholder support #21

Closed kropper closed 3 years ago

kropper commented 3 years ago

Like abbr gico=git commit -m "$1" and then jump to position $1 after expanding.

olets commented 3 years ago

Neat idea! Tricky because someone might use $1 in an abbreviation but not want it to be a placeholder, something like

abbr x="| awk '{print $1}'"

where you want literal $1

Any idea how that would work?

olets commented 3 years ago

I've decided to not support this for now. It would require some limitation on what strings could be used in an expansion, and it would mean abbr expand <abbreviation> could have a different result from <abbreviation>[Space]. Not impossible to code, but means users would have to know about more edge cases. Thanks for sharing the idea, I'll keep it in the back of my mind.

For your example, where there's just one placeholder and it's almost at the end of the expansion, I think the best solution is something like

% abbr m="git commit -m"

For cases where the placeholder is further from the end of the command, or where there are multiple placeholders, you can use xargs:

% abbr -g m='| xargs -I{} git commit -m "{}" && some other commands…'
% echo this is message m[Enter]
# runs `git commit -m "this is the message" && some other commands…`
% # in this one, note the escaped inner double quotes and escaped `$`s
% abbr -g 01="| xargs -I{} zsh -c 'input=\"{}\" && echo Open the \${input%.*} please, \${input#*.}'"
% echo pod bay doors.HAL 01[Enter]
Open the pod bay doors please, HAL
%
olets commented 4 months ago

@kropper this was added in v5.4.0!

Docs: https://zsh-abbr.olets.dev/essential-commands.html#use-cursor-markers

olets commented 2 months ago

Belatedly acknowledging more contributors in https://github.com/olets/zsh-abbr#community and https://zsh-abbr.olets.dev/community/

Would you like to be added for the placeholder idea, @kropper? If so, I'll have the all-contributors bot open a pull request. Then I'll tag you in it to review for correct info