Macos – Enter key prints ^M in certain situations in iTerm


I am using iTerm2 on a Macbook Pro running MacOS Mojave.

In certain situations, such as when doing a git add -p, pressing return ends up printing ^M instead of performing the action.

Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? ^M

Actual demo output

That means I am unable to take any action on a hunk as I cannot submit any of the given choices.

I though this could be because of how git handles returns, so I modified the core.autocrlf config but none of its valid values cause any change in this behaviour.

Then when trying other methods I relaized that this problem is visible only when using iTerm and NOT the Terminal app despite using zsh in both.

What configuration should I modify to allow for the return key to behave normally in this case?

Best Answer

This problem occurs because of odd terminal line settings. The stty tool on Unix can help you get this fixed. I have had the same issue on Iterm2 on Mac and got it fixed using.

stty sane

This command fixes terminal oddities and make it usable. Here's a good read for more info.