Son of a Typewriter

It is said that the sins of the parents will be visited upon the sons. Never been so true as in the IT industry.

An interesting case you may had the pleasure to experience is the whole tragedy around the End of Line (EOL) used by different operating systems.

The funniest part?! It’s all caused by a typewriter!

First of all, what’s an EOL exactly?!

An End of Line (EOL), as the name suggests, is a character used in text file at the end of the line to define a new line.

The big drama is that not all the operating systems use the same EOL.

Long story short, Windows uses CRLF while all the other Unix OSs use LF.

Everything is fine till you write text for the OS you running on. Stuff gets a little bit messier when you start sharing files.

Ok, got it… What’s the matter with the typewriter again?!

Ahahaha well…

In reality, it all started with the telegraph. Back in the days, morse code operator invented a way to indicate a new line or new section in a formal text message — the prosign BT (mnemonic break text).

Later on, the operator was replaced by teletypes and teleprinters.

It was in this scenario that our dear CRLF was invented to instruct the teletypes — actually, the typewriter within — to line feed and carriage return.

LF moved the paper up (but kept the horizontal position identical) while CR brought back the “carriage”.

As the typewriter was slow, sometimes multiple CR were sent as time buffer. Just to be sure that the typewriter was in the correct state for the next line.

Ok, but what’s the matter with the current operating systems?

Well, because in the early days, computers didn’t have monitors nor mouse or keyboard.

They were using teletypes!

That’s the reason why in the Unix world we still have /dev/tty and all the ttys commands.

On Unix systems, you can use the tty command to see which /dev/tty a terminal is using. You can even send text to a terminal echo "a" > /dev/tty001.

This is even the reason why in many programming languages we use the statement print to get some text on the monitor.

In reality print was meant to actually print on a teletype.

Carriage Return
Code to bring back the "carriage". Character `CR`, `0x0D`, `\r`
Line Feed
Code to move the paper up (but kept the horizontal position identical). Character `LF`, `0x0A`, `\n`

The End of Line (EOL) sequence (0x0D 0x0A, \r\n) is actually two ASCII characters, a combination of the CR and LF characters.

Survival Tips

That said, if your project has to work on Windows then set your editor to use CRLF.

Instead, if it has to run on Linux / Mac then set your editor to use LF.

As a defensive measure add * text=auto to .gitattributes.

echo "* text=auto" >.gitattributes

In case you’re sure about the EOL required then keep going specifying.

# Automatically normalize line endings for all text-based files
* text=auto

# For the following file types, normalize line endings to LF on
# checkin and prevent conversion to CRLF when they are checked out
# (this is required to prevent newline related issues like,
# for example, after the build script is run)
.*      text eol=lf
*.css   text eol=lf
*.html  text eol=lf
*.jade  text eol=lf
*.ts    text eol=lf
*.tsx   text eol=lf
*.js    text eol=lf
*.json  text eol=lf
*.less  text eol=lf
*.scss  text eol=lf
*.md    text eol=lf
*.sh    text eol=lf
*.txt   text eol=lf
*.xml   text eol=lf

Extra reading

If you want to dig deeper in the git commands to avoid strange surprises Tim Clem, Mind the End of Your Line is super recommended.