DEV Community

Lane Wagner
Lane Wagner

Posted on • Originally published at qvault.io on

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

broken piece of wood in snow

The post How to Get Consistent Line Breaks in VS Code (LF vs CRLF) appeared first on Qvault.

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


Enter fullscreen mode Exit fullscreen mode

CRLF:



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


Enter fullscreen mode Exit fullscreen mode

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

Hit me up on twitter @wagslane if you have any questions or comments.

Follow me on Dev.to: wagslane

The post How to Get Consistent Line Breaks in VS Code (LF vs CRLF) appeared first on Qvault.

Top comments (6)

Collapse
 
jacobdb profile image
Jacob Bearce

Would be nice if there was an option to automatically change existing files too, drives me nuts having to constantly fix it when dealing with Windows files...

Collapse
 
joelbonetr profile image
JoelBonetR 🥇

using linux or WSL2 on windows you can simply use the dos2unix to bulk change that for the entire project :)

Collapse
 
jacobdb profile image
Jacob Bearce

That's a good idea, thanks! Not quite as convenient as it doing it automatically, but I can probably just alias code to do dos2unix first automatically

Thread Thread
 
joelbonetr profile image
JoelBonetR 🥇

God please no. Simply configure your VSCode to use LF first, then run the dos2unix command recursively only once to edit the current files.

All edits and new files should be LF after that.

Collapse
 
ouzkagan profile image
ouzkagan
Collapse
 
noor_codes profile image
Noorullah Ahmadzai • Edited

Thank You!
I was looking for this option in vscode!