4

I have been exploring terminfo and stty. If I run stty -a|grep icanon it appears that canonical mode is set, but if I try to turn it off with stty -icanon and check again, I see that it is still set. When I do the same thing under bash, I am able to disable icanon, but I don't notice any change in behaviour. So I have 2 questions; why am I unable to disable icanon in zsh, and how should it affect the behaviour if I could?

6

When I do the same thing under bash, I am able to disable icanon, but I don't notice any change in behaviour.

That's because bash turns the canonical mode off when reading commands from the user (in order to be able to implement line editing features not offered by the terminal driver -- like inserting text, moving the cursor left and right with the arrow keys, etc), and then restores the previous terminal settings upon running any command line.

To see its effect, run an external command like cat:

bash$ stty -icanon
bash$ cat
hheelloo

Each key will be repeated, once because stty echo is on, and once because cat will immediately read it and write it back, instead of having to wait for complete lines.

[with zsh] if I try to turn it off with stty -icanon and check again, I see that it is still set.

When restoring the previous terminal settings as described above, zsh is also enforcing some sane defaults (which include the echo and icanon settings). Probably that's because it assumes that, if a command left the canonical mode off upon terminating, it was just an accident (e.g. as when a full screen app like vi or less crashed).

I wasn't able to find any zsh option which would make it work like bash (and like any other program using the readline library), but if you want to turn the icanon mode off before running a command, you can do it by calling stty from the same command line:

zsh$ stty -icanon; cat
hheelloo
| improve this answer | |
  • 1
    "Probably that's because it assumes that, if a command left the canonical mode off upon terminating, it was just an accident". Yes, zsh has been setting ICANON and ECHO to "sanitize the tty" (but otherwise leaving alone the other settings as set by the user) since around 1994 (2.4 or about). – Stéphane Chazelas Sep 25 at 17:31
  • 1
    The thing that this answer could state more clearly is that line editors like ZLE, editline, and GNU Readline switch the line discipline into a different mode for much the same reason as vi et al. do, and this is what pretty much anything with a line editor will do, from shells to bc. So this is why there's no apparent change in behaviour. Quite a number of programs aren't using "cooked" input. – JdeBP Sep 25 at 17:52
  • OK, so just to clarify; both zsh & bash run the line editor with icanon off, but when I run the stty command they temporarily turn icanon on before stty prints its output, unless it was already explicitly turned off in bash. Furthermore, it's not possible to turn icanon on for line editing in either zsh or bash, since this would disable some of the extra line editing commands that they provide. Correct? – Ben Sep 25 at 21:22
  • @Ben exactly, except that bash or zsh not being able to turn the icanon flag off will completely break their line editors -- they will have to fall back to the one implemented by the tty (enter, backspace/^H, ^W, ^U and not much more). – zevzek Sep 26 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.