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.
-
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) orCmd+,
(macOS).
- On Windows/Linux, go to
-
Search for
EoL
: In the search bar at the top of the Settings tab, typeEoL
orFiles: Eol
. -
Select
\n
(LF): Locate theFiles: 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.
- Open a File: Open the file in VS Code whose line endings you want to change.
- Locate Status Bar: Look at the bottom right corner of the VS Code window, in the status bar. You will see either
LF
orCRLF
. - Click to Change: Click on
CRLF
(if it's currently set to Windows style). A dropdown menu will appear, allowing you to selectLF
to convert the file to Unix line endings. - Save the File: After changing the line ending, save the file (
Ctrl+S
orCmd+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.
- Open Workspace Settings: With your project folder open, go to
File > Preferences > Settings
(orCode > Settings > Settings
) and then select the "Workspace" tab. - Search for
EoL
: TypeEoL
in the search bar. - Set
Files: Eol
: Choose\n
from the dropdown menu for theFiles: 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.