zsh-users
 help / color / mirror / code / Atom feed
* Select Whole, When There are Multiple Lines
@ 2021-09-25 11:29 Ahmad Ismail
  2021-09-26  7:08 ` Daniel Shahaf
  0 siblings, 1 reply; 4+ messages in thread
From: Ahmad Ismail @ 2021-09-25 11:29 UTC (permalink / raw)
  To: Zsh Users

Hi,

I am using the following to select the whole line.

_select-whole-line() {
zle beginning-of-line
((REGION_ACTIVE)) || zle set-mark-command
zle end-of-line
}
zle -N _select-whole-line
bindkey '^A' _select-whole-line # Paste

It works well when there is a single line.

But when the prompt looks like:
....
% the_command established fact that a reader will
be distracted by the readable content of a page whe
n looking at its layout. The point of using Lorem Ips
um is that it has a more-or-less normal distributi
....

I mean when there are multiple lines on the prompt, the keybinding
only works for the line where the cursor is. How can I select all
lines using ctrl-A

Thanks and Best Regards,
Ahmad Ismail


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

* Re: Select Whole, When There are Multiple Lines
  2021-09-25 11:29 Select Whole, When There are Multiple Lines Ahmad Ismail
@ 2021-09-26  7:08 ` Daniel Shahaf
  2021-09-27  7:17   ` Ahmad Ismail
  0 siblings, 1 reply; 4+ messages in thread
From: Daniel Shahaf @ 2021-09-26  7:08 UTC (permalink / raw)
  To: Ahmad Ismail, Zsh Users

Ahmad Ismail wrote on Sat, 25 Sep 2021 11:29 +00:00:
> I mean when there are multiple lines on the prompt, the keybinding
> only works for the line where the cursor is. How can I select all
> lines using ctrl-A

This works:

f() { MARK=0; CURSOR=$#BUFFER; REGION_ACTIVE=1; }
zle -N f
bindkey '^T' f
echo foo^V^Jecho bar^T    # In this line, ^X means to press Ctrl+X

It doesn't work in some other cases.  It works when $BUFFER is
multiline and $PREBUFFER is empty, but it doesn't work when $BUFFER is
single-line and the previous content is in $PREBUFFER.  You might be
able to simulate that with 'P' ranges in region_highlight and
overriding widgets that operate on the region, but it might be easier
to just use edit-command-line.


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

* Re: Select Whole, When There are Multiple Lines
  2021-09-26  7:08 ` Daniel Shahaf
@ 2021-09-27  7:17   ` Ahmad Ismail
  2021-09-27  8:40     ` Ahmad Ismail
  0 siblings, 1 reply; 4+ messages in thread
From: Ahmad Ismail @ 2021-09-27  7:17 UTC (permalink / raw)
  To: Daniel Shahaf; +Cc: Zsh Users

I did not notice it earlier. The following also works.
....
_select-whole() {
    zle beginning-of-buffer-or-history
    ((REGION_ACTIVE)) || zle set-mark-command
    zle end-of-buffer-or-history
}

zle -N _select-whole
bindkey '^A' _select-whole
....
However, it works weirdly when used on single line.

What can I do so that _select-whole works on both single and multi-lines?

Thanks and Best Regards,
Ahmad Ismail

On Sun, Sep 26, 2021 at 7:08 AM Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
>
> Ahmad Ismail wrote on Sat, 25 Sep 2021 11:29 +00:00:
> > I mean when there are multiple lines on the prompt, the keybinding
> > only works for the line where the cursor is. How can I select all
> > lines using ctrl-A
>
> This works:
>
> f() { MARK=0; CURSOR=$#BUFFER; REGION_ACTIVE=1; }
> zle -N f
> bindkey '^T' f
> echo foo^V^Jecho bar^T    # In this line, ^X means to press Ctrl+X
>
> It doesn't work in some other cases.  It works when $BUFFER is
> multiline and $PREBUFFER is empty, but it doesn't work when $BUFFER is
> single-line and the previous content is in $PREBUFFER.  You might be
> able to simulate that with 'P' ranges in region_highlight and
> overriding widgets that operate on the region, but it might be easier
> to just use edit-command-line.


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

* Re: Select Whole, When There are Multiple Lines
  2021-09-27  7:17   ` Ahmad Ismail
@ 2021-09-27  8:40     ` Ahmad Ismail
  0 siblings, 0 replies; 4+ messages in thread
From: Ahmad Ismail @ 2021-09-27  8:40 UTC (permalink / raw)
  To: Daniel Shahaf; +Cc: Zsh Users

Here is the complete thing. Seems to be working. Please let me know if
you find any issues.

....
function _home-key() {
REGION_ACTIVE=0
zle beginning-of-line
}

function _end-key() {
REGION_ACTIVE=0
zle end-of-line
}

zle -N _home-key
zle -N _end-key

bindkey '^[OH' _home-key # Home key to go to the beginning of the line.
bindkey '^[OF' _end-key # End key to go to the end of the line.

function _ctrl-home-key() {

REGION_ACTIVE=0

if [ "$BUFFERLINES" -gt 1 ]; then
zle beginning-of-buffer-or-history
else
zle beginning-of-line
fi
}

function _ctrl-end-key() {
REGION_ACTIVE=0
if [ "$BUFFERLINES" -gt 1 ]; then
zle end-of-buffer-or-history
else
zle end-of-line
fi
}

zle -N _ctrl-home-key
zle -N _ctrl-end-key

bindkey '^[[1;5H' _ctrl-home-key # ctrl + home, go to beginning when
there are multiple lines)
bindkey '^[[1;5F' _ctrl-end-key # ctrl + end, go to end when there are
multiple lines

_select-whole() {
REGION_ACTIVE=0

if [ "$BUFFERLINES" -gt 1 ]; then
zle beginning-of-buffer-or-history
((REGION_ACTIVE)) || zle set-mark-command
zle end-of-buffer-or-history
else
zle beginning-of-line
((REGION_ACTIVE)) || zle set-mark-command
zle end-of-line
fi
}

zle -N _select-whole
bindkey '^A' _select-whole # Paste

function _shift-home-key() {
REGION_ACTIVE=0
zle set-mark-command
zle beginning-of-line
}

function _shift-end-key() {
REGION_ACTIVE=0
zle set-mark-command
zle end-of-line
}

zle -N _shift-home-key # select till the beginning of line
zle -N _shift-end-key # select till the end of line

# '[[S;9H' & '[[S;9E' are defined in alacritty.yml

bindkey '[[S;9H' _shift-home-key # Shift+Home to select till the
beginning of the line
bindkey '[[S;9E' _shift-end-key # Shift+End to select till the end of the line.

function _ctrl-shift-home-key() {
REGION_ACTIVE=0
zle set-mark-command

if [ "$BUFFERLINES" -gt 1 ]; then
zle beginning-of-buffer-or-history
else
zle beginning-of-line
fi
}

function _ctrl-shift-end-key() {
REGION_ACTIVE=0
zle set-mark-command
if [ "$BUFFERLINES" -gt 1 ]; then
zle end-of-buffer-or-history
else
zle end-of-line
fi
}

zle -N _ctrl-shift-home-key
zle -N _ctrl-shift-end-key

bindkey '^[[1;6H' _ctrl-shift-home-key # ctrl + shift + home, select
till the beginning when there are multiple lines
bindkey '^[[1;6F' _ctrl-shift-end-key # ctrl + shift + end, select
till the end when there are multiple lines

....

Thanks and Best Regards,
Ahmad Ismail

On Mon, Sep 27, 2021 at 7:17 AM Ahmad Ismail <ismail783@gmail.com> wrote:
>
> I did not notice it earlier. The following also works.
> ....
> _select-whole() {
>     zle beginning-of-buffer-or-history
>     ((REGION_ACTIVE)) || zle set-mark-command
>     zle end-of-buffer-or-history
> }
>
> zle -N _select-whole
> bindkey '^A' _select-whole
> ....
> However, it works weirdly when used on single line.
>
> What can I do so that _select-whole works on both single and multi-lines?
>
> Thanks and Best Regards,
> Ahmad Ismail
>
> On Sun, Sep 26, 2021 at 7:08 AM Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
> >
> > Ahmad Ismail wrote on Sat, 25 Sep 2021 11:29 +00:00:
> > > I mean when there are multiple lines on the prompt, the keybinding
> > > only works for the line where the cursor is. How can I select all
> > > lines using ctrl-A
> >
> > This works:
> >
> > f() { MARK=0; CURSOR=$#BUFFER; REGION_ACTIVE=1; }
> > zle -N f
> > bindkey '^T' f
> > echo foo^V^Jecho bar^T    # In this line, ^X means to press Ctrl+X
> >
> > It doesn't work in some other cases.  It works when $BUFFER is
> > multiline and $PREBUFFER is empty, but it doesn't work when $BUFFER is
> > single-line and the previous content is in $PREBUFFER.  You might be
> > able to simulate that with 'P' ranges in region_highlight and
> > overriding widgets that operate on the region, but it might be easier
> > to just use edit-command-line.


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

end of thread, other threads:[~2021-09-27  8:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-25 11:29 Select Whole, When There are Multiple Lines Ahmad Ismail
2021-09-26  7:08 ` Daniel Shahaf
2021-09-27  7:17   ` Ahmad Ismail
2021-09-27  8:40     ` Ahmad Ismail

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