如何在 Visual Studio 类 Unix 代码中的所有文件中制作所有行结尾 (EOL)?

How can I make all line endings (EOLs) in all files in Visual Studio Code, Unix-like?

我使用Windows 10 Home并且我通常使用Visual Studio代码(VS代码)来编辑LinuxBash脚本以及PHP和JavaScript.

我没有开发任何专用于 Windows 的东西,我不介意我编辑的所有文件的默认 EOL 都是 Unix 类 (nix)。

我如何确保所有文件中的所有 EOL(来自任何文件扩展名)在 Visual Studio 代码中都是 nix?


我在 Windows 中使用 Visual Studio 代码编写了一些 Bash 脚本,并将它们作为项目的一部分上传到 GitHub 之后,我问了这个问题,一位审查该项目的高级程序员告诉我,我有 Windows EOLs 还有一个 BOM 问题如果我将那里的 EOL 更改为 nix(或者至少我是这么理解的),可以解决这个问题。


因为我所有的开发都是面向Linux的,所以我希望默认情况下,我编辑的任何文件都会有nix EOL,即使它是Windows独一无二。

在您的项目首选项中,add/edit以下配置选项:

"files.eol": "\n"

这是在提交 639a3cb 时添加的,因此您显然需要在该提交之后使用一个版本。

注意:即使文件中只有一个CRLF,上面的设置也会被忽略,整个文件都会被转换为CRLF。你首先需要将所有CRLF转换成LF才能用Visual Studio代码打开。

另请参阅:https://github.com/Microsoft/vscode/issues/2957

解释了如何对所有文件执行此操作(在设置中使用 files.eol),但是如果您需要覆盖该设置,则可以单击右下角的指示器打开并更改这个文件。我花了一段时间才注意到这是可点击的。

现有的两个答案都有帮助,但不是我需要的。我想将工作区中的所有换行符从 CRLF 批量转换为 LF。

我做了一个简单的扩展来做到这一点

https://marketplace.visualstudio.com/items?itemName=vs-publisher-1448185.keyoti-changeallendoflinesequence

其实这里有扩展代码供参考

'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");

            }
        }

    });

}

转换现有文件的行结尾

我们可以使用 dos2unix in WSL 或在您的 Shell 终端中。

安装工具:

sudo apt install dos2unix

转换当前目录中的行结尾:

find -type f -print0 | xargs -0 dos2unix

如果您希望从转换中排除某些文件夹,请使用:

find -type f \
     -not -path "./<dir_to_exclude>/*" \
     -not -path "./<other_dir_to_exclude>/*" \
     -print0 | xargs -0 dos2unix

对于其他人问你可以使用 “Files.eol”设置是 Visual Studio 更改每个文件的行结尾的代码。

"Files.eol": "\n"   // Unix
"Files.eol": "\r\n" // Windows

您可以在 Visual Studio 代码设置中找到该选项。 它在“文本编辑器”→“文件”→“Eol”下。 在这里你可以 select 无论你想要 \n 或 \r\n 还是 auto.

我搜索了几天的简单解决方案,但在我发现一些 Git 命令将所有文件从 CRLF 更改为 LF 后没有任何成功。

作为,确保在执行以下命令之前提交更改。

在根文件夹中键入以下内容。

git config core.autocrlf false

git rm --cached -r .         # Don’t forget the dot at the end

git reset --hard

我刚刚在 Windows 机器上遇到了同样的问题。每次我打开一个文件时,它都会将 EOL 设置为 CRLF(即使我按照人们的建议明确设置了 lf 的配置)。看起来,问题是我用 错误的 Git 配置 克隆了我的存储库。默认情况下是 CRLF。不管怎样,这就是我所做的,而且效果很好。我的工作区中不再 CRLF

  1. 使用命令 git config --global core.autocrlf false
  2. 将 Git 配置设置为 lf
  3. 现在再次克隆您的项目:git clone ...
  4. 设置你的Visual Studio代码实例,菜单文件首选项设置文件Eol 到“\n”。
  5. 打开工程,一切正常

卸载打字稿

运行

npm install -g typescript
git config --global core.autocrlf false

检查

git config --global core.autocrlf 

如果显示错误。尝试克隆和 re-run 项目