zsh-users
 help / color / mirror / code / Atom feed
* Perform history expansion on the current word only
@ 2023-06-13 13:14 Marlon Richert
  2023-06-13 13:52 ` Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: Marlon Richert @ 2023-06-13 13:14 UTC (permalink / raw)
  To: Zsh Users

All built-in widgets that perform history expansion seem to perform it
on the entire line. However, I would like to have keybinding that
performs history expansion on the current word only. How do I make
this happen?


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Perform history expansion on the current word only
  2023-06-13 13:14 Perform history expansion on the current word only Marlon Richert
@ 2023-06-13 13:52 ` Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2023-06-13 13:52 UTC (permalink / raw)
  To: Zsh Users

> On 13/06/2023 14:14 Marlon Richert <marlon.richert@gmail.com> wrote:
> All built-in widgets that perform history expansion seem to perform it
> on the entire line. However, I would like to have keybinding that
> performs history expansion on the current word only. How do I make
> this happen?

This is !-history, right?  Someone may have sneakier ideas than I do, but
I don't think this can be done simply because the history mechanism itself
just reads through a line from left to right.

It's possible to write a widget that divides a line into shell
words and only applies history to that word, however.  It would get
a bit confused if you were trying to extract words from the current line,
but I suspect that isn't the point here.  match-words-by-style can
help with this --- it's documented in zshcontrib.

The second problem is that the only existing !-history expansion
functions, as you noted, expand the whole line, so we need to shrink
the line temporarily.

Somewhat to my surprise, the following initial go does in fact seem to
be in roughly the right area.  This is expanding strictly the word
*before* the cursor --- match-words-by-style is written to allow
the caller to be clever about context, hence the long array of
returned line portions, but I've skipped that here.

Save as a function, autoload, and "zle -N" as usual.

pws


emulate -L zsh
setopt extendedglob

autoload -Uz match-words-by-style

local curcontext=":zle:$WIDGET"
local -a matched_words

match-words-by-style

BUFFER="${matched_words[2]}"
zle expand-history
LBUFFER="${matched_words[1]}$BUFFER" RBUFFER="${matched_words[3,7]}"


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-06-13 13:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-13 13:14 Perform history expansion on the current word only Marlon Richert
2023-06-13 13:52 ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).