如何在Visual Studio Code,UNIX中的所有文件中创建所有行结尾(EOL)?

use*_*970 18 php bash editor line-endings visual-studio-code

我使用Windows 10 home,我通常使用Visual Studio Code(VSCODE)来编辑Linux Bash脚本以及PHP和JavaScript.

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

在VSCODE中,如何确保所有文件中的所有EOL(来自任何文件扩展名)都是nix?


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


因为我的所有开发都是面向Linux的,所以我希望默认情况下,我编辑的任何文件都有nix EOL,即使它是Window唯一的.

hai*_*dar 34

我搜索了天,简单的解决方案,并没有任何成功后,我发现,改变了所有文件的一些Git命令CRLFLF

正如Mats指出的,确保在执行以下命令之前提交更改。

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

git config core.autocrlf false

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

git reset --hard
Run Code Online (Sandbox Code Playgroud)

  • 确保在运行之前提交任何更改!:) (8认同)
  • 如果我早点看到这个评论就好了。 (6认同)

Ian*_*wan 31

我遇到了同样的问题 - 在Windows上编辑文件通常用于Unix服务器(使用强大的ftp-sync插件)并且几乎总是想要LF行结尾.我花了很长时间才注意到右下角的当前设置,如果点击它,你可以切换当前文件的设置.

请参阅右下方消息栏中的CRLF

  • 这仅设置当前文件。问题是如何为所有文件设置它。 (23认同)
  • 我赞成接受的答案,它确实直接回答了这个问题。这个答案的目的是指出,如果默认情况下有例外,有一种简单的机制可以更改仅一个文件的文件结尾。它至少对某些人(可能是 vscode 的新手)一定有帮助,并且他们会投票?我想作为对已接受答案的评论会更好吗? (3认同)

小智 13

您可以在 Visual Studio Code 设置中找到该选项。它在“文本编辑器”?“文件”?“Eol”下。您可以在此处选择是否需要 \n 或 \r\n 或自动。

在此处输入图片说明


Jes*_*sta 11

转换现有文件的行尾

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

安装工具:

sudo apt install dos2unix
Run Code Online (Sandbox Code Playgroud)

转换当前目录中的行尾:

find -type f -print0 | xargs -0 dos2unix
Run Code Online (Sandbox Code Playgroud)

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

find -type f \
     -not -path "./<dir_to_exclude>/*" \
     -not -path "./<other_dir_to_exclude>/*" \
     -print0 | xargs -0 dos2unix
Run Code Online (Sandbox Code Playgroud)


Mik*_*ike 10

在项目首选项中,添加/编辑以下配置选项:

"files.eol": "\n"
Run Code Online (Sandbox Code Playgroud)

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

注意:即使CRLF文件中只有一个,上述设置也会被忽略,整个文件将转换为CRLF。您首先需要将其全部转换CRLFLFVisual Studio Code中的内容。

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

  • 此方法不会转换所有文件,而是转换单个文件 (4认同)

Vik*_*dub 8

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

\n
    \n
  1. lf使用以下命令设置 Git 配置git config --global core.autocrlf false
  2. \n
  3. 现在再次克隆您的项目:git clone ...
  4. \n
  5. 设置 Visual Studio Code 实例,菜单File \xe2\x86\x92 Preferences \xe2\x86\x92 Settings \xe2\x86\x92 Files : Eol为“\\n”。
  6. \n
  7. 打开项目,一切应该都符合预期
  8. \n
\n


Jim*_*ica 7

现有的两个答案都有帮助,但不是我需要的。我想将工作区中的所有换行符从 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");

            }
        }

    });

}
Run Code Online (Sandbox Code Playgroud)


小智 5

对于其他询问您可以使用“Files.eol”设置的人来说,Visual Studio Code 可以更改每个文件的行尾。

"Files.eol": "\n"   // Unix
"Files.eol": "\r\n" // Windows
Run Code Online (Sandbox Code Playgroud)


小智 5

我知道这已经晚了,但我最近正在寻找一种简单的方法,而无需在我的系统上安装任何其他东西。

如果您安装了 Windows 版 Git,则只需在所需文件夹中打开 Git Bash 窗口并运行默认捆绑的 dos2unix:

find . -name "*.filetype" -exec dos2unix {} \;
Run Code Online (Sandbox Code Playgroud)

它会为你将所有结局转换为 LF。如果您懒惰并尝试仅使用 -name "*",它也会忽略二进制文件

它帮助了我,希望对其他人也有帮助


归档时间:

查看次数:

14365 次

最近记录:

7 年,8 月 前