Joe
Joe

Reputation: 3620

Detect enter keypress from read in ZSH

How do I detect an enter keypress in ZSH with the read built-in? That is, how do I make the snippet below print "Got enter"

# Read 1 char.
read -k 1 "REPLY?$Make fooBar? [Yn]: "

if [[ "$REPLY" == '\n' ]]; then
  print "Got enter"
else
  print "Got other char: '$REPLY'"
fi

Context: I'm building a slightly more flexible yes-no prompter than what read -q in ZSH offers.

Upvotes: 2

Views: 1088

Answers (1)

chepner
chepner

Reputation: 531235

Use ANSI quoting:

if [[ $REPLY == $'\n' ]]; then

$'...' is like single quotes, but certain escaped characters have special meaning: \n is a linefeed character, \t is a tab, \\ is a literal backslash, \' is a literal single quote, etc.

Upvotes: 2

Related Questions