Reputation: 1
I use Windows 10 Home and I usually use Visual Studio Code (VS Code) to edit Linux Bash scripts as well as PHP and JavaScript.
I don't develop anything dedicated for Windows and I wouldn't mind that the default EOLs for all files I edit whatsoever would be Unix like (nix).
How could I ensure that all EOLs, in all files whatsoever (from whatever file extension), are nix, in Visual Studio Code?
I ask this question after I've written a few Bash scripts in Windows with Visual Studio Code, uploaded them to GitHub as part of a project, and a senior programmer that reviewed the project told me I have Windows EOLs there and also a BOM problem that I could solve if I'll change the EOLs there to be nix (or that's what I understood, at least).
Because all my development is Linux-oriented, I would prefer that by default, any file I edit would have nix EOLs, even if it's Windows unique.
Upvotes: 160
Views: 207923
Reputation: 24403
(From 20218) In your project preferences, add/edit the following configuration option:
"files.eol": "\n"
This was added as of commit 639a3cb, so you would obviously need to be using a version after that commit.
Note: Even if you have a single CRLF
in the file, the above setting will be ignored and the whole file will be converted to CRLF
. You first need to convert all CRLF
into LF
before you can open it in Visual Studio Code.
See also: https://github.com/Microsoft/vscode/issues/2957
Note (Nov 2024): In the most recent versions of VS Code the EOL setting is under File/Preferences/Settings
. Once there enter eol
in the settings filter, then choose the \n
option. No amount of changing Git settings Git cache fixed the issue for me until I changed this setting in VS Code.
Once you have changed the settings in VS Code, make sure your command line is set to your current project or directory where you want files converted to LF end of line characters and use the following command:
find . -type f -print0 | xargs -0 dos2unix -ic0 | xargs -0 dos2unix -b
. This will update all files within that directory from CRLF to LF and print the list of files updated to the command line. Note you may need to run sudo apt install dos2unix
first to install the dos2unix utility.
Upvotes: 92
Reputation: 432
git config core.autocrlf false
git rm --cached -r . # Don’t forget the dot at the end
git reset --hard
Running these commands will work, Please commit all your changes
Upvotes: -1
Reputation: 1386
If you are using Prettier in your frontend project - add this to your .prettierrc
config:
"endOfLine": "crlf",
Then simply run:
prettier --write
Upvotes: 2
Reputation: 87
I know this is late to the game but I was recently looking for a simple method without having to install anything else to my system.
If you have Git for windows installed then just open a Git Bash window in the desired folder and run dos2unix which appears to be bundled by default:
find . -name "*.filetype" -exec dos2unix {} \;
and it will convert all your endings to LF for you. It also ignores binary files if you get lazy and try and just use -name "*"
It helped me, hopefully helps others
Upvotes: 6
Reputation: 12519
We can use dos2unix in WSL or in your Shell terminal.
Install the tool:
sudo apt install dos2unix
Convert line endings in the current directory:
find -type f -print0 | xargs -0 dos2unix
If there are some folders that you'd want to exclude from the conversion, use:
find -type f \
-not -path "./<dir_to_exclude>/*" \
-not -path "./<other_dir_to_exclude>/*" \
-print0 | xargs -0 dos2unix
Edited in 2023
To install PowerShell 7 on Windows, follow the instructions here.
You will need to have Chocolatey installed, and to install dos2unix for Chocolatey.
Install Chocolatey following this instructions if you don't have it already.
Install dos2unix for Chocolatey
Open a PowerShell terminal as Administrator
pwsh.exe -new_console:a
Install dos2unix
choco install dos2unix
Close the Administrator terminal
exit
Now, we can use dos2unix
from PowerShell.
# Set the parameters
$path = Get-Location
$numbOfThreads = 50
$exclude_patterns = "^.*(\.git|\.idea|node_modules|\.next|\.(jpeg|jpg|png|gif|mp4|mkv|mp3|wav)$).*$"
# Find files to convert
$files = Get-ChildItem -Path $path -Recurse -Include *.* -File -Force | Where-Object {$_.FullName -notmatch $exclude_patterns}
Write-Host "Found $($files.Count) files"
# Convert the files
$files | ForEach-Object -Parallel {
dos2unix $_.FullName
} -ThrottleLimit $numbOfThreads
Upvotes: 34
Reputation: 835
First, ensure the line endings settings on your vscode is set to crlf
.
If you want this as a global setting press ctr+shift+p
and type user settings json
and select the first option to open the global settings file. If you just want it on a specific project create a settings.json
file inside a .vscode
folder at the base of your project. Then add this line there.
...
"files.eol": "\r\n"
This doesn't solve the problem though. after every pull or after launching vscode the changes still show up. Just running a git add .
will show no more changes which I think is what is desired but you don't want to do this every time.
To solve this, you need a .editorconfig file at the base of your project. In it, you can add:
[*]
...
end_of_line = crlf
Upvotes: 1
Reputation: 1799
I searched for a simple solution for days and didn't have any success until
I found some Git commands that changed
all files from CRLF
to LF
.
As pointed out by Mats, make sure to commit changes before executing the following commands.
In the root folder type the following.
git config core.autocrlf false
git rm --cached -r . # Don’t forget the dot at the end
git reset --hard
Upvotes: 161
Reputation: 2506
Uninstall typescript
run
npm install -g typescript
git config --global core.autocrlf false
check
git config --global core.autocrlf
if it shows false. try to clone and re-run the project
Upvotes: 0
Reputation: 451
I've just faced the same issue on my Windows machine. Every time I opened a file it would set the EOL to CRLF
(even though I explicitly set up a configuration for lf
as people suggested). It appeared, that the problem is that I cloned my repository with the wrong Git configuration. It was CRLF
by default. Anyway, here's what I did and it worked perfectly. No more CRLF
in my workspace.
lf
with the command git config --global core.autocrlf false
git clone ...
Upvotes: 9
Reputation: 303
You can find the option in Visual Studio Code settings. It's under "Text Editor"→"Files"→"Eol". Here you can select whether you want \n or \r\n or auto.
Upvotes: 29
Reputation: 99
For other people asking you can use the "Files.eol" setting is Visual Studio Code to change the line ending for every file.
"Files.eol": "\n" // Unix
"Files.eol": "\r\n" // Windows
Upvotes: 8
Reputation: 3799
The accepted answer explains how to do this for all files (use files.eol in settings), but if you ever need to override that setting there's an indicator at the bottom right that you can click on and change for this one file. Took me a while to notice that this was clickable.
Upvotes: 185
Reputation: 5026
Both existing answers are helpful but not what I needed. I wanted to bulk convert all the newline characters in my workspace from CRLF to LF.
I made a simple extension to do it
In fact, here is the extension code for reference
'use strict';
import * as vscode from 'vscode';
import { posix } from 'path';
export function activate(context: vscode.ExtensionContext) {
// Runs 'Change All End Of Line Sequence' on all files of specified type.
vscode.commands.registerCommand('keyoti/changealleol', async function () {
async function convertLineEndingsInFilesInFolder(folder: vscode.Uri, fileTypeArray: Array<string>, newEnding: string): Promise<{ count: number }> {
let count = 0;
for (const [name, type] of await vscode.workspace.fs.readDirectory(folder)) {
if (type === vscode.FileType.File && fileTypeArray.filter( (el)=>{return name.endsWith(el);} ).length>0){
const filePath = posix.join(folder.path, name);
var doc = await vscode.workspace.openTextDocument(filePath);
await vscode.window.showTextDocument(doc);
if(vscode.window.activeTextEditor!==null){
await vscode.window.activeTextEditor!.edit(builder => {
if(newEnding==="LF"){
builder.setEndOfLine(vscode.EndOfLine.LF);
} else {
builder.setEndOfLine(vscode.EndOfLine.CRLF);
}
count ++;
});
} else {
vscode.window.showInformationMessage(doc.uri.toString());
}
}
if (type === vscode.FileType.Directory && !name.startsWith(".")){
count += (await convertLineEndingsInFilesInFolder(vscode.Uri.file(posix.join(folder.path, name)), fileTypeArray, newEnding)).count;
}
}
return { count };
}
let options: vscode.InputBoxOptions = {prompt: "File types to convert", placeHolder: ".cs, .txt", ignoreFocusOut: true};
let fileTypes = await vscode.window.showInputBox(options);
fileTypes = fileTypes!.replace(' ', '');
let fileTypeArray: Array<string> = [];
let newEnding = await vscode.window.showQuickPick(["LF", "CRLF"]);
if(fileTypes!==null && newEnding!=null){
fileTypeArray = fileTypes!.split(',');
if(vscode.workspace.workspaceFolders!==null && vscode.workspace.workspaceFolders!.length>0){
const folderUri = vscode.workspace.workspaceFolders![0].uri;
const info = await convertLineEndingsInFilesInFolder(folderUri, fileTypeArray, newEnding);
vscode.window.showInformationMessage(info.count+" files converted");
}
}
});
}
Upvotes: 11