`read` command returns immediately instead of waiting for input

69 Views Asked by At

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?

1

There are 1 best solutions below

4
chepner On

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
}