Ora

How Do I Configure VS Code to Use Unix Line Endings?

Published in VS Code Configuration 3 mins read

To configure VS Code to use Unix line endings (LF or \n), you need to adjust the Files: Eol setting within its preferences. This ensures all new files you create will automatically adopt the Unix-style line ending.

Understanding Line Endings

Line endings are special characters that mark the end of a line in a text file. Different operating systems historically used different conventions, which can sometimes lead to issues when collaborating across platforms.

Line Ending Type Character Description Common Operating Systems
Unix/Linux/macOS LF (\n) Line Feed Unix, Linux, macOS
Windows CRLF (\r\n) Carriage Return + Line Feed Windows

For cross-platform development, especially with version control systems like Git, using Unix line endings (LF) is often preferred to avoid inconsistencies and potential conflicts.

Configuring Unix Line Endings in VS Code

VS Code offers flexibility in managing line endings, allowing you to set a global default for new files and also change them for individual existing files or within specific workspaces.

1. Setting a Global Default for New Files

This configuration applies to all new files you create in VS Code, ensuring they default to Unix line endings.

  1. Open VS Code Settings:

    • On Windows/Linux, go to File > Preferences > Settings.
    • On macOS, go to Code > Settings > Settings.
    • Alternatively, use the keyboard shortcut Ctrl+, (Windows/Linux) or Cmd+, (macOS).
  2. Search for EoL: In the search bar at the top of the Settings tab, type EoL or Files: Eol.

  3. Select \n (LF): Locate the Files: Eol setting. From its dropdown menu, choose \n.

    This explicitly sets new files to use Unix-style line endings.
    The setting applies to all new files that you create, ensuring they consistently adopt the \n character for line breaks.

2. Changing Line Endings for an Existing File

You can change the line endings of an open file directly from the VS Code status bar.

  1. Open a File: Open the file in VS Code whose line endings you want to change.
  2. Locate Status Bar: Look at the bottom right corner of the VS Code window, in the status bar. You will see either LF or CRLF.
  3. Click to Change: Click on CRLF (if it's currently set to Windows style). A dropdown menu will appear, allowing you to select LF to convert the file to Unix line endings.
  4. Save the File: After changing the line ending, save the file (Ctrl+S or Cmd+S) for the change to take effect.

3. Configuring Per-Workspace Line Endings

For projects that require specific line ending conventions, you can configure this setting on a per-workspace basis. This overrides the global setting for that particular project.

  1. Open Workspace Settings: With your project folder open, go to File > Preferences > Settings (or Code > Settings > Settings) and then select the "Workspace" tab.
  2. Search for EoL: Type EoL in the search bar.
  3. Set Files: Eol: Choose \n from the dropdown menu for the Files: Eol setting.

Alternatively, you can manually edit your workspace's settings.json file. This file is located in the .vscode folder within your project directory.

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

This configuration ensures that anyone working on this specific project will use Unix line endings by default within VS Code, promoting consistency among team members.

For more detailed information on VS Code settings, you can refer to the official VS Code documentation on User and Workspace Settings.