LF vs CRLF - Configure Git and VS Code on Windows to use Unix line endings

nausaf - Sep 5 - - Dev Community

Why LF should be configured as End of Line character in

There are two different character sequences that are used in text files to indicate a line break, i.e. end of the current line and beginning of the next one:

  • Text editors (and code editors) on Unix-based operating systems, such as Linux and Mac OS, embed a character known as Line Feed (ASCII Code 10, Unicode character code also the same, written as 0x000A in hexadecimal) in the text to indicate a line break. The name of this character is usually abbreviated to LF.

  • Text- and code editors on Windows typically embed a sequence of two characters at the location of a line break in text: a Carriage Return character (CR for short; ASCI code 13, in Unicode it is the same and written in hexadecimal as 0x000D) followed by a Line Feed. This character sequence is usually referred to as CRLF.

Characters/character sequences to indicate a line breaks are also referred to as End of Line (EOL) characters or as Line Endings.

In programming languages (such as JavaScript, C#) the Line Feed character is escaped as \n whereas Carriage Return Character is escaped as \r. In order to see the effect of LF, open F12 Developer Toolbar in the browser. In this go to Console. Type console.log("first line\nsecond line\nthis is the third line") and press Enter:

Image description

You can see that the LF character (escaped using \n in the line of JavaScript code shown) translates into a line break.

Now type the same line with \r\n in places where there was \n, i.e. type the following line in Console and press Enter:

console.log("first line\r\nsecond line\r\nthis is the third line")
Enter fullscreen mode Exit fullscreen mode

This too displays as three separately lines, i.e. the character sequence CRLF was printed as a line break.

Image description

Both LF and CRLF work as line break when you print text from a programming language, regardless of the operating system on which the code executes. So the two lines of code given above should work on both Windows and on Linux/Unix/MacOS, in Node.js as well in the browser.

However, when editors and text tools on Unix-based systems load files which were authored in Windows and use CRLF line endings, they can have trouble displaying or processing them. On the other hand, all modern word processors and code editors on Windows can handle the Unix LF line endings perfectly well.

Since teams members may use different operating systems and since open source projects can accept contributions from developers using a variety of operating systems, it is best practice to ensure that all text files (including code files) in a Git repo use Unix line endings (i.e. LF, and not CRLF).

Code scaffolders such as create-next-app typically generate files with LF line endings, whether they are run on Windows or on other operating systems. If you open a code file generated by such a scaffolder and look in the status bar in your VS Code, you should see LF, indicating that the line endings used in the file would be LF.

Image description

However, if you create a new (empty) file, this would default to CRLF on Windows. Since we want line endings in all files in a Git repo to be LF, VS Code needs to be configured to use LF for all new files.

Also, Git on Windows also uses CRLF line ending by default. What this means is that it would replace all standalone LF characters (those not prefixed with the CR character) with CRLF when committing your changes or when fetching a remote repo. So even if all of the files in the local Git working directory had LF endings, when you run git add., you would get a warning for every file that was added to the local repo's staging area which says that LF will be replaced by CRLF the next time Git touches that file:

Image description

Therefore, Git on Windows also needs to be configured to use LF as the EOL character instead of CRLF.

VS Code and Git configuration

To set the default for line endings in VS Code to LF:

  • go to Command Palette (Ctrl + Shift + P)
  • type Settings. Choose Preferences: Open Settings (UI) from the list of options that are displayed
  • In the Search settings textbox on the Settings tab, type eol
    This would bring up the end of line setting:

    Image description

  • From the dropdown, select \n. This would default VS Code to always use LF line endings in new files.

    Image description

  • Close Settings tab.

Every new file created in VS Code on Windows would now use LF as the end of line character.

To configure Git on Windows to use LF line endings when committing staged files or when fetching from a remote repo:

  • If it does not already exist, create a .gitattributes file in root of the repo's working directory.

  • Add the following to the .gitattributes file:

    * text=auto eol=lf
    
  • Commit your changes:

    git add .
    git commit -m "Configured .gitattributes with LF line endings"
    

The git add . command and fetch from remote should now happen smoothly without warnings about LF and CRLF.

. . . . .
Terabox Video Player