如何在Visual Studio Code中统一设置所有文件为类Unix换行符?
How to Force VS Code to Use Unix (LF) Line Endings for All Files on Windows 10 Home
Since you’re working exclusively with cross-platform code (Bash, PHP, JS) and don’t need Windows-specific CRLF line endings, here’s how to set LF as the default for every new file—plus fix existing ones if needed:
1. Set Default Line Endings via GUI
- Open VS Code and go to File > Preferences > Settings (or press
Ctrl+,to open settings directly). - In the search bar, type
files.eolto find the "Files: Eol" setting. - From the dropdown menu, select
\n(LF - Unix and macOS). This will make LF the default for all new files, no matter their extension.
2. Edit settings.json for Precise Control
If you prefer editing the config file directly:
- Open the Command Palette with
Ctrl+Shift+P, type Preferences: Open Settings (JSON), and hit Enter. - Add or update this line in the JSON object:
"files.eol": "\n" - Save the file. Now every new file you create in VS Code will use Unix-style line endings automatically.
3. Convert Existing Files to LF
If you have old files using CRLF:
- Open the file in VS Code. Look at the bottom-right corner of the window—you’ll see either
CRLForLFdisplayed. - Click on that label, then select LF from the popup menu to convert the file’s line endings.
- To convert all files in a folder at once, use the Command Palette: search for Change End of Line Sequence and choose LF. This will apply the change to every file in your workspace.
4. Avoid Accidental CRLF Switching
To ensure VS Code doesn’t override your settings when opening files:
- Add this line to your settings.json:
"files.autoGuessEncoding": false
This prevents VS Code from guessing line endings based on the file’s content, keeping your LF default intact.
内容的提问来源于stack exchange,提问作者Osi




