bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: word selection in history expansion - !?word?%


From: Jess M
Subject: Re: word selection in history expansion - !?word?%
Date: Mon, 12 Aug 2024 14:15:35 -0700

Given the age of the behaviour, I would agree on not making it the default
behaviour.

To update the documentation to reflect that, how about something like this:
--- current --- lib/readline/doc/hsuser.texi
 @item %
 The first word matched by the most recent @samp{?@var{string}?} search,
if the search string begins with a character that is part of a word.
--- proposed ---
 @item %
 The first word matched by the most recent @samp{?@var{string}?} search,
if the search string begins with a character that is part of a word. The
search happens in reverse, so if multiple words on a line match, the word
nearer the end of the line is returned.
---

As for providing an option to alter the search behaviour:

The initial surprise was that a forward-line search should happen (rather
than the observed reverse line search).  In practice, just having a firm
understanding of the exact behaviour, is enough for my needs, I can
compensate my usage accordingly.  The option to to change the search
direction feels rather limited.  If the code is to be updated, I'd look for
something more flexible.

Consider updating the word designator to accept a count.  In this mode,
`!?word?:%3` would provide the third found item found in a reverse search.

Example:
```
$ echo example-line01-word01 example-line01-word02
example-line01-word01 example-line01-word02
$ echo example-line02-word01 example-line02-word02
example-line02-word01 example-line02-word02
$ echo !?example?:%2
echo example-line02-word01
example-line02-word01
```

A consideration would be how to handle multiple lines.

We could error:
```
$ echo example-line01-word01 example-line01-word02
example-line01-word01 example-line01-word02
$ echo example-line02-word01 example-line02-word02
example-line02-word01 example-line02-word02
$ echo !?example?:%3
bash: !?example?:%3: event not found
```

Alternatively, we could look on previous lines
```
$ echo example-line01-word01 example-line01-word02
example-line01-word01 example-line01-word02
$ echo example-line02-word01 example-line02-word02
example-line02-word01 example-line02-word02
$ echo !?example?:%3
echo example-line01-word02
example-line01-word02
```
As there are not three matches on the first found line, we continue
counting across previous lines.

Implementing the search across lines might prove tricky.  The current code
splits line match and word match; we don't even count words until after
we've identified the matched line.


-- Jess M

On Mon, Aug 12, 2024 at 12:43 PM Chet Ramey <chet.ramey@case.edu> wrote:

> On 8/9/24 2:15 AM, Jess M wrote:
> > Observed behaviour:
> > ```
> > $ echo word01 word02 word03 floogle
> > word01 word02 word03 floogle
> > $ echo !?word?%
> > echo word03
> > word03
> > $ # I expected to get word01
> > $ echo $BASH_VERSION
> > 5.2.26(1)-release
> > ```
> >
> > The bash manual for word designators sounds to me as if word01 should be
> > selected.
>
> That's certainly the csh behavior.
>
> Bash has done it this way for as long as its history expansion has been
> implemented. It dates back to at least 1988-89. I think the idea (that
> was a long time ago) was that it started as a way to perform searching for
> readline, and in that case you want the history to basically be treated as
> a single long string, in which you search backwards or forwards from the
> current location. The csh-style history expansion code inherited that
> behavior.
>
> It should not be difficult to provide an option to do it the csh way, but
> there's a lot of existing behavior there, so I don't know about making it
> the default.
>
> --
> ``The lyf so short, the craft so long to lerne.'' - Chaucer
>                  ``Ars longa, vita brevis'' - Hippocrates
> Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]