0

I'm trying to make a simple zsh widget that asks user for a string and sets it as the current command prompt afterwards

zle -N replace-command-buffer
bindkey '\eg' replace-command-buffer

replace-command-buffer() {
  local input
  echo "Enter a string: "
  read -r input
  BUFFER="$input"
  zle reset-prompt
}

But read command returns immediately without waiting for input. How do I fix that?

Poma
  • 8,174
  • 18
  • 82
  • 144
  • Functions executed like this have their standard input redirected from `/dev/null`. You can only use `read -k` or `read -q`. (See `man zshzle`, under the heading "USER-DEFINED WIDGETS".) – chepner Feb 13 '23 at 15:33
  • It's not entirely clear what you are trying to do; `reset-prompt` doesn't use `BUFFER` to define the prompt; it simply reevaluates the existing values of `PS1`, `RSP1`, etc and redisplays them, then puts the contents of `BUFFER` back on the command line. – chepner Feb 13 '23 at 15:34
  • I want a custom prompt below the command line, then after a user enters some text, replace the buffer with something – Poma Feb 13 '23 at 16:17

1 Answers1

0

Functions executed by zle have their standard input redirected from /dev/null, so your shell's standard input isn't available.

What you probably want is to execute the recursive-edit widget, which will set BUFFER itself.

replace-command-buffer () {
    printf "Enter a string: "
    zle recursive-edit
    zle reset-prompt
}
chepner
  • 497,756
  • 71
  • 530
  • 681
  • How do I do it below the buffer? similar to how history search works – Poma Feb 13 '23 at 16:16
  • Something involving `zsl-isearch-update`, maybe? See `zle -M` as well. – chepner Feb 13 '23 at 16:23
  • If you replace `printf` with `zle -M`, you'll get the prompt below the line, but typing will still occur in the original buffer. (Maybe neither `recursive-edit` nor `reset-prompt` would be necessary, depending on what else you want to do with the subsequent input.) – chepner Feb 13 '23 at 16:27
  • I use currently use `read-from-minibuffer` but it applies shell rules like syntax highlighting which I don't want. Printing below the prompt is easier, but for reading I didn't find a proper way to do it. – Poma Feb 13 '23 at 16:36