Windows and Linux EoL Sequence — Configure VS Code and Git
If you have modified and uploaded any code to Git on both Linux and Windows systems, and have encountered issues with line endings, then this article is for you.
Editing code on both Windows and Linux/Mac OS systems can become a little tricky some times. EoL (End of Line) Sequence that these systems use can cause some issues how some tools read our code. In this article I will highlight few things what we can do to overcome these issues..
The term CRLF refers to Carriage Return (ASCII 13, \r) Line Feed (ASCII 10, \n). And Windows and Unix based systems use different sequence to determine End of Line.
Windows (or in other words DOS) uses Carriage Return and Line Feed (“\r\n”) as a line ending.
and Unix/Linux/Mac OS uses just Line Feed (“\n”).
When we modify these files on our systems (Windows and Linux/Mac OS), which mostly depend on the Editor we are using to modify the files And/OR System configuration which determines how we process these files..
In our explicit scenario, we discuss about two tools. VS Code and Git.
When we talk about a highly customizable editor like VS Code, we can configure it as to how we set this EoL (End of Line) sequence.
VS Code => Settings => Files: EoL
And choose “\n” as EoL character if you would like your files to have Unix Style line endings and choose “\r\n” if you would like your files to have Windows Style line endings.
That settings applies to all new files that you create.
Sometimes, if you download a repo from GitHub and start editing those files on your Windows systems, it automatically converts those files to Windows format and can mess with your Eol sequence.
To know what Eol sequence is in use, you can look at the bottom status bar for your VS Code to check that.. see screenshot below..
the CRLF at the bottom indicates that this shell script is having Windows line endings. Click on that CRLF and you get the ability to change the Eol for this file. and here is how it looks.
So, if you choose EL, then your file status at the bottom of that page changes to EL, indicating that now your file has Unix style line endings.
CAUTION: This will only change the Eol Setting for only this current file you are working on. It will not automatically make this change for all the files in your current Repo Directory.
In General, there are few Git specific global configuration settings you can do to ensure how your system deals with Eol sequence.
There is a git global configuration that you can set to take care of this.
The above command when executed ensures that it automatically handles the Eol sequence. When you download your code from GitHub repo onto your local, it uses the local System (windows or Linux) line endings and when you check back the changes in with a commit, it will use default (Unix) line endings.
You can go more granular as to what files should have what Eol by specifying the details in a .gitattributes file in the home directory of your Repo. And here is GitHub help link explaining more details as to how to configure the same.. (without me duplicating that effort)..
https://help.github.com/en/github/using-git/configuring-git-to-handle-line-endings
And finally to convert your files from one format to the other, there are many utilities available.. something like dos2unix, unix2dos etc. And this can as well be accomplished using other tools/programming languages like vi, sed, awk, perl, python etc..