zsh-users
 help / color / mirror / code / Atom feed
* zsh-no-ps2: A plugin that inserts a newline on Enter instead of displaying PS2
@ 2023-11-12 13:48 Roman Perepelitsa
  2023-11-12 14:00 ` zeurkous
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Roman Perepelitsa @ 2023-11-12 13:48 UTC (permalink / raw)
  To: Zsh Users

tl;dr: zsh-no-ps2 changes the behavior of Enter key to insert a
newline instead of going into PS2

    git clone https://github.com/romkatv/zsh-no-ps2.git ~/zsh-no-ps2
    source ~/zsh-no-ps2/zsh-no-ps2.plugin.zsh

Normally, when you press Enter in zsh, one of two things happens: if
the currently typed command is complete, zsh attempts to execute it;
if it's incomplete, zsh displays the secondary prompt, also known as
PS2, to invite you to continue typing the command. I hate when zsh
goes into PS2 because to fix the command most of the time I need to
*edit what I've already typed*, which cannot be done. Those of you who
are using zsh4humans have been able to change this behavior so that
Enter inserts a newline if the typed command is incomplete. I've just
created a standalone plugin to make this feature available to
everybody: https://github.com/romkatv/zsh-no-ps2. No more PS2!

Also posted to r/zsh:
https://www.reddit.com/r/zsh/comments/17tgjit/zshnops2_a_plugin_that_inserts_a_newline_on_enter/

Roman.


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

* RE: zsh-no-ps2: A plugin that inserts a newline on Enter instead of displaying PS2
  2023-11-12 13:48 zsh-no-ps2: A plugin that inserts a newline on Enter instead of displaying PS2 Roman Perepelitsa
@ 2023-11-12 14:00 ` zeurkous
       [not found] ` <6550dda9.170a0220.4fe72.dd3bSMTPIN_ADDED_BROKEN@mx.google.com>
  2023-11-13  7:29 ` Stephane Chazelas
  2 siblings, 0 replies; 9+ messages in thread
From: zeurkous @ 2023-11-12 14:00 UTC (permalink / raw)
  To: Roman Perepelitsa, Zsh Users

Haai,

On Sun, 12 Nov 2023 14:48:22 +0100, Roman Perepelitsa <roman.perepelitsa@gmail.com> wrote:
> tl;dr: zsh-no-ps2 changes the behavior of Enter key to insert a
> newline instead of going into PS2
>
>     git clone https://github.com/romkatv/zsh-no-ps2.git ~/zsh-no-ps2
>     source ~/zsh-no-ps2/zsh-no-ps2.plugin.zsh
>
> Normally, when you press Enter in zsh, one of two things happens:

Might be worth noting that this is also the case with 'Return', which,
confusingly, produces a carriage return. ('Enter' does only do so by
later convention; the latter really means "submit data for immediate
processing", which is a different operation on mainframes).

> if
> the currently typed command is complete, zsh attempts to execute it;
> if it's incomplete, zsh displays the secondary prompt, also known as
> PS2, to invite you to continue typing the command. I hate when zsh
> goes into PS2 because to fix the command most of the time I need to
> *edit what I've already typed*, which cannot be done.

Interrupt, then recall the command from the history buffer. It's a
workaround, but it works :)

> Those of you who
> are using zsh4humans have been able to change this behavior so that
> Enter inserts a newline if the typed command is incomplete. I've just
> created a standalone plugin to make this feature available to
> everybody: https://github.com/romkatv/zsh-no-ps2. No more PS2!

An alternative is to define an external editor (grep for
'edit-command-line' in zshcontrib(1)), which will be available during
PS2 as well.

HTH,

        --zeurkous.

-- 
Friggin' Machines!


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

* Re: zsh-no-ps2: A plugin that inserts a newline on Enter instead of displaying PS2
       [not found] ` <6550dda9.170a0220.4fe72.dd3bSMTPIN_ADDED_BROKEN@mx.google.com>
@ 2023-11-12 18:59   ` Bart Schaefer
  2023-11-12 19:18     ` zeurkous
  2023-11-13 14:20     ` Michal Politowski
  0 siblings, 2 replies; 9+ messages in thread
From: Bart Schaefer @ 2023-11-12 18:59 UTC (permalink / raw)
  To: zeurkous; +Cc: Roman Perepelitsa, Zsh Users

On Sun, Nov 12, 2023 at 6:14 AM <zeurkous@blaatscaahp.org> wrote:
>
> On Sun, 12 Nov 2023 14:48:22 +0100, Roman Perepelitsa <roman.perepelitsa@gmail.com> wrote:
> > tl;dr: zsh-no-ps2 changes the behavior of Enter key to insert a
> > newline instead of going into PS2
> >
> >     git clone https://github.com/romkatv/zsh-no-ps2.git ~/zsh-no-ps2
> >     source ~/zsh-no-ps2/zsh-no-ps2.plugin.zsh
> >
> > Normally, when you press Enter in zsh, one of two things happens:
>
> Might be worth noting that this is also the case with 'Return', which,
> confusingly, produces a carriage return. ('Enter' does only do so by
> later convention; the latter really means "submit data for immediate
> processing", which is a different operation on mainframes).

Interesting as a historical curiosity, but I don't recall zsh ever
running on a mainframe, and typical keyboards have for decades now
produced either '\r' or '\n' pretty much interchangeably for either or
both of Return and Enter, so zsh has always treated the two as
equivalent unless instructed not to.

> > I hate when zsh
> > goes into PS2 because to fix the command most of the time I need to
> > *edit what I've already typed*, which cannot be done.
>
> Interrupt, then recall the command from the history buffer.

This is also what the push-line-or-edit ZLE widget simulates.  I
always rebind ESC-q to push-line-or-edit, leaving ESC-(shift)Q for
push-line.

The drawback to skipping PS2 is that you don't get the helpful hints
about where you are in the syntax tree (by having %_ in the value of
PS2 or RPS2), but as Roman notes in his README.md, you can get back to
that by forcing an accept-line.


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

* RE: Re: zsh-no-ps2: A plugin that inserts a newline on Enter instead of displaying PS2
  2023-11-12 18:59   ` Bart Schaefer
@ 2023-11-12 19:18     ` zeurkous
  2023-11-13 14:20     ` Michal Politowski
  1 sibling, 0 replies; 9+ messages in thread
From: zeurkous @ 2023-11-12 19:18 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Roman Perepelitsa, Zsh Users

On Sun, 12 Nov 2023 10:59:36 -0800, Bart Schaefer <schaefer@brasslantern.com> wrote:
>> > Normally, when you press Enter in zsh, one of two things happens:
>>
>> Might be worth noting that this is also the case with 'Return', which,
>> confusingly, produces a carriage return. ('Enter' does only do so by
>> later convention; the latter really means "submit data for immediate
>> processing", which is a different operation on mainframes).
>
> Interesting as a historical curiosity, but I don't recall zsh ever
> running on a mainframe, and typical keyboards have for decades now
> produced either '\r' or '\n' pretty much interchangeably for either or
> both of Return and Enter, so zsh has always treated the two as
> equivalent unless instructed not to.

Still, it's perhaps best to speak in terms of what zsh reads (which has
usually been filtered by termios(4) or a similar line discipline),
instead of in terms of how keys are labeled (often erroneously these
days...) on the actual keyboard. 

>> Interrupt, then recall the command from the history buffer.
>
> This is also what the push-line-or-edit ZLE widget simulates.  I
> always rebind ESC-q to push-line-or-edit, leaving ESC-(shift)Q for
> push-line.

That appears useful, thanks.

        --zeurkous.

-- 
Friggin' Machines!


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

* Re: zsh-no-ps2: A plugin that inserts a newline on Enter instead of displaying PS2
  2023-11-12 13:48 zsh-no-ps2: A plugin that inserts a newline on Enter instead of displaying PS2 Roman Perepelitsa
  2023-11-12 14:00 ` zeurkous
       [not found] ` <6550dda9.170a0220.4fe72.dd3bSMTPIN_ADDED_BROKEN@mx.google.com>
@ 2023-11-13  7:29 ` Stephane Chazelas
  2023-11-13  8:46   ` Roman Perepelitsa
  2023-11-14  1:38   ` Bart Schaefer
  2 siblings, 2 replies; 9+ messages in thread
From: Stephane Chazelas @ 2023-11-13  7:29 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Zsh Users

On 2023-11-12 13:48, Roman Perepelitsa wrote:
> tl;dr: zsh-no-ps2 changes the behavior of Enter key to insert a
> newline instead of going into PS2
[...]

I remember offering something similar at 
https://unix.stackexchange.com/questions/750378/disabling-the-secondary-prompt-in-zsh/750481#750481 
not so long ago (you might want to offer your plugin as an alternative 
answer there).

I agree that the inability to easily edit the previous line is quite 
annoying. Another problem with the PS2 prompt is that you cannot 
copy-paste the full command from your terminal.

That's one of the things fish does better than zsh IMO (though I'm not 
convinced by its indenting of continuation lines which also means the 
code cannot be copy-pasted).

Maybe that's something that could be implemented as a new widget in ZLE? 
An about (from Bart):

> The drawback to skipping PS2 is that you don't get the helpful hints
> about where you are in the syntax tree (by having %_ in the value of
> PS2 or RPS2), but as Roman notes in his README.md, you can get back to
> that by forcing an accept-line.

Another widget that returns the state of the parser after reading what's 
left of the cursor?

-- 
Stephane


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

* Re: zsh-no-ps2: A plugin that inserts a newline on Enter instead of displaying PS2
  2023-11-13  7:29 ` Stephane Chazelas
@ 2023-11-13  8:46   ` Roman Perepelitsa
  2023-11-14  2:10     ` Bart Schaefer
  2023-11-14  1:38   ` Bart Schaefer
  1 sibling, 1 reply; 9+ messages in thread
From: Roman Perepelitsa @ 2023-11-13  8:46 UTC (permalink / raw)
  To: Stephane Chazelas; +Cc: Zsh Users

On Mon, Nov 13, 2023 at 8:29 AM Stephane Chazelas <stephane@chazelas.org> wrote:
>
> On 2023-11-12 13:48, Roman Perepelitsa wrote:
> > tl;dr: zsh-no-ps2 changes the behavior of Enter key to insert a
> > newline instead of going into PS2
> [...]
>
> I remember offering something similar at
> https://unix.stackexchange.com/questions/750378/disabling-the-secondary-prompt-in-zsh/750481#750481
> not so long ago (you might want to offer your plugin as an alternative
> answer there).

Thanks for the link. I've posted my own answer there.

There are two differences in my code compared to yours. Firstly, my
code looks at $PREBUFFER$BUFFER rather than just $BUFFER. This makes a
difference if you end up in PS2 for some reason, perhaps
intentionally.  I think it's worth editing your answer to fix this.
Secondly, my code works as expected when the buffer is "for x" or
"<<END", while yours ends up in PS2. The fix is rather hacky: append
$'\ndo\ndone' and see if that parses. It's possible that there are
other corner cases which this hack does not cover. I'll be very
interested to know about them if you find any.

My function that detects whether a piece of code is well-formed can be
found here: https://github.com/romkatv/zsh4humans/blob/v5/fn/-z4h-is-valid-list

> > The drawback to skipping PS2 is that you don't get the helpful hints
> > about where you are in the syntax tree (by having %_ in the value of
> > PS2 or RPS2), but as Roman notes in his README.md, you can get back to
> > that by forcing an accept-line.
>
> Another widget that returns the state of the parser after reading what's
> left of the cursor?

Or perhaps something more generic: an actual parser, which gives you
an AST. This would do all the heavy lifting required for implementing
a syntax highlighter and a multitude of other features.

Roman.


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

* Re: zsh-no-ps2: A plugin that inserts a newline on Enter instead of displaying PS2
  2023-11-12 18:59   ` Bart Schaefer
  2023-11-12 19:18     ` zeurkous
@ 2023-11-13 14:20     ` Michal Politowski
  1 sibling, 0 replies; 9+ messages in thread
From: Michal Politowski @ 2023-11-13 14:20 UTC (permalink / raw)
  To: Zsh Users

Dnia Sun, 12 Nov 2023 10:59:36 -0800, Bart Schaefer napisał(a):
> On Sun, Nov 12, 2023 at 6:14 AM <zeurkous@blaatscaahp.org> wrote:
[...]
> > Interrupt, then recall the command from the history buffer.
> 
> This is also what the push-line-or-edit ZLE widget simulates.  I
> always rebind ESC-q to push-line-or-edit, leaving ESC-(shift)Q for
> push-line.

Oh, nice. I don't really feel the need for push-line as opposed to push-input
so I just have ^Q rebound to the latter and it's almost as good for me,
given how rarely I require the equivalent of the -or-edit.

-- 
Michał Politowski
Talking has been known to lead to communication if practiced carelessly.


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

* Re: zsh-no-ps2: A plugin that inserts a newline on Enter instead of displaying PS2
  2023-11-13  7:29 ` Stephane Chazelas
  2023-11-13  8:46   ` Roman Perepelitsa
@ 2023-11-14  1:38   ` Bart Schaefer
  1 sibling, 0 replies; 9+ messages in thread
From: Bart Schaefer @ 2023-11-14  1:38 UTC (permalink / raw)
  To: Stephane Chazelas; +Cc: Roman Perepelitsa, Zsh Users

On Sun, Nov 12, 2023 at 11:29 PM Stephane Chazelas
<stephane@chazelas.org> wrote:
>
> I agree that the inability to easily edit the previous line is quite
> annoying.

As discussed at some length in the "feep in the night-time" thread
(2021/04), this is because the input goes directly into the parser at
each accept-line, so to change the PREBUFFER you have to cancel the
parse-in-progress.

> Another problem with the PS2 prompt is that you cannot
> copy-paste the full command from your terminal.

This is why one uses e.g. PS2='' RPS2=' <%_' with transient_rprompt.


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

* Re: zsh-no-ps2: A plugin that inserts a newline on Enter instead of displaying PS2
  2023-11-13  8:46   ` Roman Perepelitsa
@ 2023-11-14  2:10     ` Bart Schaefer
  0 siblings, 0 replies; 9+ messages in thread
From: Bart Schaefer @ 2023-11-14  2:10 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Stephane Chazelas, Zsh Users

On Mon, Nov 13, 2023 at 12:46 AM Roman Perepelitsa
<roman.perepelitsa@gmail.com> wrote:
>
> On Mon, Nov 13, 2023 at 8:29 AM Stephane Chazelas <stephane@chazelas.org> wrote:
> >
> > Another widget that returns the state of the parser after reading what's
> > left of the cursor?
>
> Or perhaps something more generic: an actual parser, which gives you
> an AST.

Theoretically I think something along those lines could be extracted
from the wordcode (before the parser discards it in the event of an
incomplete structure) but I'm not the ideal person to attempt that.


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

end of thread, other threads:[~2023-11-14  2:11 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-12 13:48 zsh-no-ps2: A plugin that inserts a newline on Enter instead of displaying PS2 Roman Perepelitsa
2023-11-12 14:00 ` zeurkous
     [not found] ` <6550dda9.170a0220.4fe72.dd3bSMTPIN_ADDED_BROKEN@mx.google.com>
2023-11-12 18:59   ` Bart Schaefer
2023-11-12 19:18     ` zeurkous
2023-11-13 14:20     ` Michal Politowski
2023-11-13  7:29 ` Stephane Chazelas
2023-11-13  8:46   ` Roman Perepelitsa
2023-11-14  2:10     ` Bart Schaefer
2023-11-14  1:38   ` Bart Schaefer

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).