broken piece of wood in snow

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

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).

Michael Scott condescending to the Windows OS

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”:

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.

See also: Lint on Save With VS Code Official Golang Extension

Thanks For Reading

Follow us on Twitter @q_vault if you have any questions or comments

Take game-like coding courses on Qvault Classroom

Subscribe to our Newsletter for more educational articles

%d bloggers like this: