2

I have a zle widget like:

_ctrl-a() {
  CURSOR=0
  if ((MARK == 0)); then
    zle set-mark-command
  fi

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

zle -N _ctrl-a
bindkey '^A' _ctrl-a # Ctrl+A

It selects the whole buffer. I mean it put cursor at the beginning of the buffer, start marking then put the cursor at the end of buffer.

Here, the widget I am using for multi-line command is end-of-buffer-or-history, however, I want it to be end-of-buffer.

So, I want to replace:

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

with

zle end-of-buffer

But there seems to be no widget for end-of-buffer.

What can I do?

P.S. It would be a good learning experience if I could select the whole buffer using MARK variable.

1

1 Answer 1

1

In widget code, you can not only read but also modify variables like MARK, CURSOR, BUFFER, etc.

To move the cursor to the end of the buffer:

CURSOR=$#BUFFER

To select the whole buffer with the mark at the beginning and the cursor at the end, you don't need to call any widget: just set the appropriate variables.

function _ctrl-a {
  MARK=0
  CURSOR=$#BUFFER
  REGION_ACTIVE=1
}
6
  • If i replace REGION_ACTIVE=1 with zle exchange-point-and-mark only then it works. Jun 1, 2023 at 18:55
  • The lines that work for me are ((CURSOR = 0)); ((MARK = $#BUFFER)); zle exchange-point-and-mark Jun 1, 2023 at 19:08
  • @AhmadIsmail The code I posted works with zsh -f, in any of emacs/viins/vicmd, with zsh 5.8.1. There may be configurations that could break that code, but I can't think of any. Jun 1, 2023 at 19:24
  • The code you provided misses first word in every line. pasteboard.co/TvzMxjbhFzJ9.png I know it should not happen, but it is happening. I am using zsh 5.8 . This does not happen when i use the code which i have provided above. I do not know the explanation. Jun 1, 2023 at 19:32
  • 1
    @AhmadIsmail That's presumably due to some setting in your .zshrc, but I have no idea what it could be. Jun 1, 2023 at 19:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .