zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: Perform history expansion on the current word only
Date: Tue, 13 Jun 2023 14:52:48 +0100 (BST)	[thread overview]
Message-ID: <141543605.1836026.1686664368186@mail.virginmedia.com> (raw)
In-Reply-To: <CAHLkEDuR6aKM1eUk1fBpUya67TO=9gvcr5+DHf8ay=T9vMCZOg@mail.gmail.com>

> 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]}"


      reply	other threads:[~2023-06-13 13:53 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-13 13:14 Marlon Richert
2023-06-13 13:52 ` Peter Stephenson [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=141543605.1836026.1686664368186@mail.virginmedia.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).