1

I execute an incorrect command over ssh:

watch /bin/df | grep _23

Now I execute a command like env and get this:

LC_TERMINAL_VERSION=3.3.4
                         LC_TERMINAL=iTerm2
                                           LC_CTYPE=en_US.UTF-8
                                                               LC_COLLATE=en_US.UTF-8
                                                                                     LC_MESSAGES=en_US.UTF-8
                                                                                                            LC_MONETARY=en_US.UTF-8
                                                                                                                                   LC_NUMERIC=en_US.UTF-8
                                                                                                                                                         LC_TIME=en_US.UTF-8
                                                                                                                                                                            LC_ALL=en_US.UTF-8
                                                                SHELL=/usr/bin/zsh                                                                                                                                                                        SSH_TTY=/dev/pts/26
   TERM=xterm-256color
                      DISPLAY=localhost:16.0
                                            XDG_SESSION_ID=18040
                                                                XDG_RUNTIME_DIR=/run/user/1000
                                                                                              SHLVL=1

It looks like the output line separator is a line feed but not carriage return.

But I'm not sure it only happens over ssh. I think I've seen this behavior after using vim too.

Why does this happen and what is the minimal way to reset it? I usually run source ~/.zshrc but that is overkill.

1
  • 2
    While not a direct answer, have you tried running the reset command? Many times I've had the console output get all janky and reset generally fixes it.
    – jhufford
    May 21, 2021 at 22:19

1 Answer 1

1

It is well known that Unix, Linux, and other Unix-like systems use a single character to represent the end of a line of text (EOL).  This character is called Newline and is often represented as \n.  This is somewhat confusing, as most software terminals (e.g., xterm) and hardware teletypes require two control characters to advance to a new line: a Carriage Return (CR) to move the cursor (or print head) to the left margin, and a Line Feed (LF) to move the cursor down one line without moving horizontally (or scroll the window up, or roll the paper up).  (The fact that Newline and LF are numerically the same byte value adds to the confusion.)

Unix-like systems handle this by translating \n to CR + LF on output in the TTY driver.  However, you might not always want that to happen.  In particular, you don’t want it to happen if you are connecting to a non-Unix system via ssh.  And vim doesn’t want it to happen; it likes to be able to use Newline as Cursor Down.  So that behavior is optional, and programs like ssh and vim disable it.  (They should re-enable it upon exit, but sometimes things don’t work correctly, e.g., if a program gets interrupted.)

Like most TTY driver options, this is controlled by stty.  The option is called onlcr, which stands for

      on Output, when you see a NewLine, also output a CR
If you do stty -a (to see all the stty settings), you’ll see onlcr (it may be near the beginning of the second-to-last line).  If you do stty -a when you’re in the weird state, you’ll see -onlcr, indicating that the option is turned off.

The simple fix for this specific problem is to type stty onlcr to turn the option back on.  But that’s an obscure initialism that may be hard to remember.  Also, if the program (e.g., ssh or vim) has messed with stty settings, it may have messed with other ones that are not as obvious.  There’s an easy-to-remember command to set all the stty settings back to normal values; it is

stty sane

As jhufford points out, some systems have a reset command that also does this.

2
  • Very nice detailed answer!
    – jhufford
    May 22, 2021 at 20:19
  • Thanks for the details, I'll be referring to this over and over in future. I'll create a script called ~/bin/newline_fix.sh so I don't have to remember the right commands. May 26, 2021 at 21:37

You must log in to answer this question.

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