How To Make Correct Line Endings by@wagslane

How To Make Correct Line Endings

image
Lane Wagner HackerNoon profile picture

Lane Wagner

Founder of Boot.dev. Whining about coding sins since 2011. Committing coding sins for the same.

Ever had the problem where you submit a pull request and the diff is waaaaay bigger than it should be? The code looks identical but GitHub is telling you that it's all different! This is typically due to a difference in line endings. Unix systems (Linux and Mac) default to the LF (line feed) character for line breaks. Windows on the other hand is "special" and defaults to CR/LF (carriage return AND line feed).

image

The Quick Fix

If you are here to quickly fix a single file that you are having problems with, you are in luck. At the bottom right of the screen in VS Code there is a little button that says "LF" or "CRLF":

image

Click that button and change it to your preference. Voila, the file you are editing now has the correct line breaks.

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",
}

CRLF:

{
    "files.eol": "\r\n",
}

If you set it in your global settings.json file it will apply to your entire machine. If you just want it set 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

Note: 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.

Thanks For Reading

Comments

Signup or Login to Join the Discussion

Tags

Related Stories