Closed burneyy closed 4 months ago
Thanks for the PR @burneyy!
~My immediate zsh-abbr priority is getting v5 out. (Long-time listeners will know it's been in the works for a while. Work and afk life is also busy, so I'm reserving the right to get delayed š However I'm actively working on a push to the finish line ā in fact that's why I'm at my computer on this lovely weekend afternoon!) I'd be happy to talk more about this and to look at your implementation after I've cut that release.~
Edit: released 5ļøā£
specify the position of the cursor after expanding the abbreviation
Neat idea! No promises yet āI want to take time to think about any possible drawbacksā but it seems useful and like a safer way of satisfying #21.
__CURSOR__
Something specific to zsh-abbr would be safer. Theoretically possible that someone would be using __CURSOR__
for something else. Maybe ABBR_EXPANSION_CURSOR
?
I think abbr-expand-and-accept
should probably cut out the cursor keyword from the expansion. What do you think?
omit the trailing whitespace
You can already do this by binding abbr-expand
~(docs link; I know it's hard to find in the very long README. v5 will come with a better docs)~ edit: link in new docs
In a sense it would be reasonable to disable abbr-expand-and-space
's space in the custom cursor location caseā¦ but instead I would expect abbr-expand-and-space
to still add space at the end of the entire expansion (not immediately before where the cursor keyword was).
I'd rather not blur the line between abbr-expand
and abbr-expand-and-space
. My initial thought is it's okay to require custom-cursor-position powerusers to use a custom binding.
Is it possible for both abbr-expand
and abbr-expand-and-space
to support the cursor position?
Immediately trigger the syntax highlighting
So far my decision has been to not build in support for any specific syntax highlighters. It adds maintenance overhead / risk.
Have you tried something like this? (in .zshrc
)
expand-abbreviation-and-highlight-line() {
abbr-expand
_zsh_highlight
}
zle -N expand-abbreviation-and-highlight-line
bindkey "mybindinggoeshere" expand-abbreviation-and-highlight-line
# load zsh-abbr here
(Untested)
(edited my comment ā burneyy if you're already here and reading, reload)
Hey @olets, thanks for the quick response!
__CURSOR__
Something specific to zsh-abbr would be safer. Theoretically possible that someone would be using
__CURSOR__
for something else. MaybeABBR_EXPANSION_CURSOR
?
I have considered that as well, however since googling for __CURSOR__
didn't lead to any reasonable results, I thought it was safe and unique enough - but indeed something a bit more verbose might be safer.
I also considered introducing an option that needs to be set when adding an abbreviation with a custom cursor position like abbr add -c myabbr="some command with a custom __CURSOR__ position"
but this would need to be saved somehow along with the abbreviations and is probably too much overhead, what do you think?
I think
abbr-expand-and-accept
should probably cut out the cursor keyword from the expansion. What do you think?
Possibly, yeah... I personally do not have a use-case for abbreviations with a custom cursor position that would be useful also when executing immediately. I do stuff like rm -rf <some_location>/__CURSOR__
where I actually prefer to keep the __CURSOR__
string when I accidentally press enter such that not the whole <some_location>
directory gets deleted. It is something to think about for sure.
omit the trailing whitespace
You can already do this by binding
abbr-expand
(docs link; I know it's hard to find in the very long README. v5 will come with a better docs).
True, but I would rather like to save this information along with the abbreviation and not having to think about it when expanding it.
In a sense it would be reasonable to disable
abbr-expand-and-space
's space in the custom cursor location caseā¦ but instead I would expectabbr-expand-and-space
to still add space at the end of the entire expansion (not immediately before where the cursor keyword was).I'd rather not blur the line between
abbr-expand
andabbr-expand-and-space
. My initial thought is it's okay to require custom-cursor-position powerusers to use a custom binding.Is it possible for both
abbr-expand
andabbr-expand-and-space
to support the cursor position?
I kind of think of pressing space to just trigger the expansion and not actually inserting a literal space. So in fact, I would even rather have to specify a space within the definition of the abbreviation than by default appending one when pressing the space button. But I guess that is just me and probably highly debatable... ;)
Immediately trigger the syntax highlighting
So far my decision has been to not build in support for any specific syntax highlighters. It adds maintenance overhead / risk.
Have you tried something like this? (in
.zshrc
)expand-abbreviation-and-highlight-line() { abbr-expand _zsh_highlight } zle -N expand-abbreviation-and-highlight-line bindkey "mybindinggoeshere" expand-abbreviation-and-highlight-line # load zsh-abbr here
(Untested)
Yes looks reasonable to do this externally, I will remove that part of the PR again I guess.
Have a nice remaining weekend!
I have now removed the syntax highlighting part from the PR.
For anyone interested, you can put the following snippet into your .zshrc
to enable it externally:
# Enable syntax highlighting after expanding with space
expand_abbreviation_and_space_and_highlight() {
_abbr_widget_expand_and_space
_zsh_highlight
}
zle -N expand_abbreviation_and_space_and_highlight
bindkey " " expand_abbreviation_and_space_and_highlight
Following our discussion, I have tweaked the implementation now as follows:
__ABBR_CURSOR__
) can be adjusted through the parameter ABBR_CURSOR_MARKER
Side note:
It allows for template-like abbreviations such as
abbr templ="first here __ABBR_CURSOR__ then here __ABBR_CURSOR__ then there __ABBR_CURSOR__"
which jumps from one cursor marker to the next after each expansion i.e. after each press of space
Caveats:
Typing in __ABBR_CURSOR__
(outside of the usage of zsh-abbr) will cause the cursor to jump to that position. Maybe it is safer to disable that functionality by default? However, I think that string is specific enough to not be typed in another context (without being aware of it at least)
the following snippet ā¦
_abbr_widget_expand_and_space
Glad this came up! _abbr_widget_expand_and_space
is supposed to be deprecated. Use abbr-expand-and-space
instead. In v5 _abbr_widget_expand_and_space
will be gone and abbr-expand-and-space
will be your only option. I've cut a new version, v4.8.1, to add deprecation warnings.
I would even rather have to specify a space within the definition of the abbreviation than by default appending one when pressing the space button
For anyone else who wants this: this is supported even without this PR's changes. Disable the default keybindings (ABBR_DEFAULT_BINDINGS=0
) and then bind space to abbr-expand
.
Glad this came up!
_abbr_widget_expand_and_space
is supposed to be deprecated. Useabbr-expand-and-space
instead. In v5_abbr_widget_expand_and_space
will be gone andabbr-expand-and-space
will be your only option. I've cut a new version, v4.8.1, to add deprecation warnings.
Got it!
For anyone else who wants this: this is supported even without this PR's changes. Disable the default keybindings (
ABBR_DEFAULT_BINDINGS=0
) and then bind space toabbr-expand
.
But unfortunately then, one cannot use space as a ānormalā space anywhere else. I guess I could add a new function abbr-expand-or-space that does what I intendedā¦
Btw, there is a big update incoming for abbreviations in fish where one of the new features is support for a custom cursor position
this is a great update! it will be very useful for my abbrs
Quick note to say other things are keeping me busy but this is still on my mind and something I'd like to eventually support
Very useful feature indeed
Thanks for your patience, all!
Replaced this with
which is includes this PR but separates the "set cursor during expansion" and "templates" into two separate features. I used %
for the default markers, to match fish (see https://fishshell.com/docs/current/cmds/abbr.html#add-subcommand).
It's released in v5.4.0
Docs:
@burneyy lmk if you'd like to be listed as a contributor in https://zsh-abbr.olets.dev/community/ and https://github.com/olets/zsh-abbr#community
@burneyy lmk if you'd like to be listed as a contributor in https://zsh-abbr.olets.dev/community/ and https://github.com/olets/zsh-abbr#community
Great that it has found its way into the release now, and yeah I would be happy to be listed among the contributors :)
@all-contributors please add @burneyy for code. (#63)
@olets
I've put up a pull request to add @burneyy! :tada:
@burneyy please see https://github.com/olets/zsh-abbr/pull/113#issuecomment-2088853394
__CURSOR__
one can specify the position of the cursor after expanding the abbreviation using spaceabbr ccr="conan create __CURSOR__ demo/testing"
places the cursor betweencreate
anddemo/testing
after the expansion (inspired by stackoverflow)abbr trm="rm -rf /tmp/__CURSOR__
")