ben*_*tah 146 command-line bash zsh
例如,如果我正在tail -f输入文件或read输入用户输入,<Enter>将^M在终端中生成而不是结束read或向标准输出添加换行符。^J工作正常。
ssh如果这很重要,我正在进入 ubuntu 系统。这在 zsh 和 bash 中都发生在我身上。我使用的一种解决方案是在 zsh 中重新映射^Mto ^J,但这似乎无法解决根本问题。有谁知道这可能是什么原因造成的?
编辑:为了回答一些问题,我正在ssh从 OSX 进入 Ubuntu。我正在使用 iTerm 和 zsh。我还忘了提到我在 Ubuntu 机器上使用 tmux。
编辑2:错过了一个问题。当我型Ctrl-V Enter我得到^M(无论在OSX和Ubuntu)。
编辑 3:在 OSX 和 Ubuntu 上echo $TERM生成screen-256color.
小智 288
如果其他人有这个问题,很可能是 stty 终端线路设置的问题,而不是 TERM 的问题。如果您再次遇到这种情况,请尝试运行stty sane并告诉我们是否可以解决问题。
Bra*_*des 45
Try running stty -a to view your terminal settings. My suspicion is that your icrnl setting is not set and will be shown as -icrnl (the minus sign means that it is turned off) instead of having its usual setting of being on. Here is how my terminal is normally set up when I log in:
$ stty -a
speed 38400 baud; rows 45; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl -ixon -ixoff
-iuclc -ixany -imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke
Run Code Online (Sandbox Code Playgroud)
And I have no problem with line endings: either return (^M) or enter (^J) will end input lines. But if I turn icrnl off then ^M codes suddenly appear each time I am talking to a program and hit enter:
$ stty -icrnl
$ read line
Line of text^M^M^M^M
$ stty -a
speed 38400 baud; rows 45; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff
-iuclc -ixany -imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke
Run Code Online (Sandbox Code Playgroud)
The code icrnl means “turn carriage returns into newlines” and hides from the running program that you might be typing ^M when Unix really wants ^J. Old keyboards used to have a separate Return and Enter key (where Return typically advanced you through a form and Enter submitted it), but today we generally have only one line-ending key and so this terminal setting helps combine the two meanings.
Add the stty icrnl command to your .profile or .bashrc if you find that this is indeed the problem setting.
小智 5
我刚刚在 Ubuntu 20.04 上使用 zsh 遇到了这个问题,但既没有也tty sane没有stty icrnl工作。
事实证明这是由于禁用造成的NumLock。
当我重新启用它后,Enter按键又恢复正常了。