How to Show and Set Line endings in Visual Studio Code

avatar
Borislav Hadzhiev

Last updated: Apr 6, 2024
4 min

banner

# Changing the end of line (EOL) sequence in VS Code

To change the end of line sequence for a specific file:

  1. Click on the "CRLF" or "LF" label in the status bar at the bottom.

change-end-of-line-sequence-for-specific-file

  1. Select your preferred end of line sequence from the dropdown menu.
MacOS and Linux use LF (LineFeed) characters for line endings, whereas Windows uses CRLF (CarriageReturn + LineFeed) characters.

If you don't see the CRLF or LF label in the status bar at the bottom, right-click on the status bar and make sure "Editor End of Line" is ticked.

tick editor end of line

# Set the end of line sequence using the Command Palette

An alternative way to set the end of line sequence is to use the Command Palette.

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type end of line and select Change End of Line Sequence.

change end of line sequence command palette

  1. Select your preferred end of line sequence from the list.

select end of line sequence

This allows you to set the line endings for the file.

# Changing the default value of the end of line sequence globally

If you want to change the default value of the end of line sequence globally:

  1. Press Ctrl + Shift + P (or Command + Shift + P on macOS).
Note: you can also press F1 to open the Command Palette.
  1. Type user settings and select Preferences: Open User Settings.

open user settings

You can also open the settings screen by pressing Ctrl + , on Windows and Linux or Cmd + , on macOS.

  1. Type file end of line and set the value of the Files: Eol setting.

set default value file end of line

The Files: Eol setting has 3 possible values:

  • auto - uses operating system-specific end of line character.
  • \n - uses LF (LineFeed) character. Suitable for macOS and Linux.
  • \r\n - uses CRLF (CarriageReturn + LineFeed) characters. Suitable for Windows.

Note that updating the setting globally only changes the default line ending character for newly created files.

It won't change the line endings for your existing files.

# Setting the line ending character for a specific project

If you only want to apply the setting to your current project:

  1. In the root directory of your project, create a .vscode folder.

  2. Create a settings.json file in the .vscode folder.

  3. Set the files.eol property in your settings.json file.

.vscode/settings.json
{ "files.eol": "\n" }

set line ending character locally

The possible values are: \n, \r\n and auto.

.vscode/settings.json
{ "files.eol": "\r\n" }

And here is an example that sets the property to auto (operating system-specific end of line character).

.vscode/settings.json
{ "files.eol": "auto" }

Updating the files.eol setting in .vscode/settings.json only applies to your current project (workspace) and overrides any global configuration of the property.

As previously noted, the setting only changes the default line ending for newly created files.

It won't change the line endings for your existing files.

# How to show Line endings in Visual Studio Code

VS Code doesn't have a native way to show line endings on each line, however, you can use an extension.

  1. Click on Extensions in the left sidebar.
  • You can also open the Extensions menu by pressing:
    • Ctrl + Shift + X on Windows or Linux.
    • Command + Shift + X on macOS.
  1. Type render line endings.

install render line endings extension

  1. Click on the Install button.

Make sure to install the correct Render Line Endings extension as shown in the screenshot.

  1. Press Ctrl + Shift + P (or Command + Shift + P on macOS).
Note: you can also press F1 to open the Command Palette.
  1. Type user settings json.

  2. Click on Preferences: Open User Settings (JSON)

preferences open user settings

  1. Add the following settings to your settings.json file.
settings.json
{ "editor.renderWhitespace": "trailing", "code-eol.newlineCharacter": "↓", "code-eol.returnCharacter": "←", "code-eol.crlfCharacter": "↵", // your other settings }

You can also set the renderWhitespace property to all to render all whitespace characters.

When you open a file, you will be able to see the EOL characters at the end of each line.

extension renders eol characters

# Changing the EOL character for all files from CRLF to LF

If you need to change the EOL character for all files from CRLF (Windows) to LF (macOS and Linux):

  1. Commit your changes before running the following commands.
  2. Set autocrlf to false by issuing the following command.
shell
git config core.autocrlf false
  1. Delete all files except those that you have edited from the current directory.
shell
git rm --cached -r .
  1. Return the deleted files to a state where they all have their LF line endings.
shell
git reset --hard

# Changing the EOL character for all files from CRLF to LF using Prettier

If you use prettier in your environment, you can also issue a command to change the EOL character for all files from CRLF to LF.

shell
npx prettier --end-of-line lf --write

You can also set the endOfLine option to lf in your .prettierrc file.

.prettierrc
"endOfLine": "lf"

Now when you issue the npx prettier --write command, it automatically converts the EOL character for all files from CRLF to LF.

shell
npx prettier --write

I've also written a guide on how to use VS Code as your default Git editor, difftool and mergetool.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.