RSVP here for our first hackathon! Free prizes, the kickoff is 8 AM PST Jan 17th

How to Get Consistent Line Breaks in VS Code (LF vs CRLF)

A learning path to your next coding job

  1. Join the Qvault community and share your career goals
  2. We'll help you find the knowledge gaps holding you back
  3. Complete recommended courses and projects
  4. Find your next opportunity with a newly polished resume

Our courses include but are not limited to

  • Golang, Python, JavaScript
  • Algorithms, data structures, cryptography
  • Graphics and functional programming

Have you ever had the problem where you submit a pull request and the diff is much larger than it should be? Maybe the code looks identical, but GitHub tells you it’s completely different?

This is typically due to a difference in line endings, especially the difference in LF vs. CRLF. Unix systems like Linux and macOS use LF, the line feed character, for line breaks by default. Windows, on the other hand, is special and uses CR/LF, carriage return AND line feed character, by default.

Michael Scott condescending to the Windows OS
Michael Scott On Windows Line Endings

Unless you work on a Windows-only team, the answer is almost always to change all your code to the Unix default of LF.

The Quick Fix for “End of line character is invalid”

If you’re here to quickly fix a single file that you’re having problems with, you’re in luck. At the bottom right of the screen in VS Code, click the little button that says LF or CRLF. After changing it to your preference, Voila, the file you’re editing now has the correct line breaks.

vscode crlf lf line endings switch
Click the LF/CRLF button to toggle line endings

The Big Fix

If you want new files to automatically have the correct line endings, then you can set the following setting in the top level of your settings.json file:

For LF:

{ "files.eol": "\n", }
Code language: JSON / JSON with Comments (json)

CRLF:

{ "files.eol": "\r\n", }
Code language: JSON / JSON with Comments (json)

If you set the above in your global settings.json file it will apply to your entire machine. If you just want the settings for the project you are working on, then edit the settings.json in the .vscode directory at the root of your project: .vscode/settings.json.

This setting will not automatically fix all files in your project that have the wrong line endings! It only applies to new ones. To fix the old ones go through and use the manual method as described in the first paragraph.

What is CRLF?

CR LF stands for “Carriage Return, Line Feed” – it’s a digital remnant of classic typewriters. With typewriters, you had to push the “carriage” (the thing that holds the paper) back into place, hence “Carriage Return”.

When everything went digital, some devices required a “Line Feed” character to terminate lines, so Microsoft decided to just make a new-line have both characters so that they would work correctly on all devices.

typewriter form unsplash with carriage

CR¬†and LF are just bytecodes. Computers store text characters as numbers in binary, just 1’s and 0s. Carriage Return (CR), is represented in ASCII (a common character encoding protocol) as 13, or in binary, 00001101. Likewise, the line feed character (LF) is 10 or 00001010.

As you can imagine, CRLF is just both bytes shoved up next to each other: 0000110100001010.

Ascii Table

Related Content

Trying to find your next programming job?

If you are a self-taught developer having trouble finding your first programming job, we've got your back! We have the learning resources and tight-knit dev community that you need to land the coding job you've been looking for. To get started, create a free account and join our Discord community.

Have questions or feedback?

If we've made a mistake in the article, please let us know so we can get it corrected!

Leave a Comment