183

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.

5
  • What are you Using to ssh to Ubuntu?
    – Tasos
    Apr 1, 2014 at 2:02
  • Yes --- and from which OS? It seems a TERM problem.
    – Rmano
    Apr 1, 2014 at 4: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 1, 2014 at 7: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 1, 2014 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 7, 2015 at 6:56

4 Answers 4

359

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.

13
  • 62
    This works on OS X as well. Mar 16, 2016 at 19:47
  • 1
    Worked for me (Mint 17.1). Aug 9, 2016 at 18:00
  • 6
    Worked on macOS Catalina ZSH. Question: Why does this only happen sometimes? And does stty sane fix it permanently or will I need to re-run this when/if it happens again? Thanks! Feb 6, 2020 at 17:14
  • 3
    @JoshuaPinter: it is a temporary fix since it is caused by programs that do not clean up properly when they exit.
    – hackerb9
    Feb 20, 2020 at 4:52
  • 5
    The secret of how you RUN stty sane when you cannot press enter would be useful here. ^J? Jun 21, 2020 at 23:35
54

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.

2
  • 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 7, 2019 at 2: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, 2020 at 0:20
5

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.

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.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .