180

For example, if I am tail -fing a file or reading user input, <Enter> will produce ^M in the terminal instead of ending the read or adding a newline to stdout. ^J works fine.

I'm sshing into the ubuntu system, if that matters. This happens to me both in zsh and bash. One solution I played with is remapping the ^M to ^J in zsh, but that seems like it wouldn't fix the root problem. Anyone know what might be causing this?

Edit: To answer some questions, I'm sshing into Ubuntu from OSX. I'm using iTerm and zsh. I also forgot to mention that I'm using tmux on the Ubuntu machine.

Edit 2: Missed a question. When I type Ctrl-V Enter I get ^M (both on OSX and Ubuntu).

Edit 3: On OSX and Ubuntu echo $TERM produces screen-256color.

benekastah
  • 1,909
  • 2
  • 12
  • 6
  • What are you Using to ssh to Ubuntu? – Tasos Apr 01 '14 at 02:02
  • Yes --- and from which OS? It seems a TERM problem. – Rmano Apr 01 '14 at 04:41
  • 1
    What do you get if you type **Ctrl** + **V** followed by **Enter**? As you are able to execute commands just fine, this is definitely not a problem of the shell. – Adaephon Apr 01 '14 at 07:39
  • 2
    An (involuntary) restart of my OSX box seems to have fixed the issue... I guess it's one of those. Hopefully it's gone for good. – benekastah Apr 01 '14 at 18:41
  • Also for me the only thing that worked was a restart including the clearance of the NVRAM. Press and hold the Command-Option-P-R keys immediately after you hear the startup sound to also reset the NVRAM of OSx – jonbros Jul 07 '15 at 06:56

4 Answers4

354

In case anybody else has this problem, it is most likely a problem with the stty terminal line setting rather than a TERM problem. If this happens to you again, try running stty sane and let us know if that fixes it.

hackerb9
  • 5,385
  • 1
  • 16
  • 14
53

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

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

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.

Brandon Rhodes
  • 1,552
  • 10
  • 8
  • 3
    I think I turned off `ircnl` by accidental key-smashing working in a live terminal session. Just ran `$ stty icrnl` and it fixed itself. – 0xbe5077ed Dec 07 '19 at 02:02
  • Not sure where to put it, but it did work. Host machine or the one I'm ssh'ed into? Too late now, already fixed it. – dansch May 31 '20 at 00:20
4

I just ran into this problem with zsh on Ubuntu 20.04 and neither tty sane nor stty icrnl worked.

As it turns out it was caused by disabling NumLock.
Once I re-enabled it the Enter key behaved normally again.

zx485
  • 2,249
  • 11
  • 24
  • 34
Martin
  • 41
  • 1
0

My solution:

CTRL+Enter is gonna solve your problem. Whenever you want to run a command on the commend line you can press CTRL+ENTER.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://askubuntu.com/help/whats-reputation) you will be able to [comment on any post](https://askubuntu.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/1234620) – David Jul 03 '22 at 02:57