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命令CRLF到LF。
在根文件夹中键入以下内容。
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)
Ian*_*wan 31
我遇到了同样的问题 - 在Windows上编辑文件通常用于Unix服务器(使用强大的ftp-sync插件)并且几乎总是想要LF行结尾.我花了很长时间才注意到右下角的当前设置,如果点击它,你可以切换当前文件的设置.
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。您首先需要将其全部转换CRLF为LFVisual Studio Code中的内容。
另请参阅:https : //github.com/Microsoft/vscode/issues/2957
我刚刚在 Windows 机器上遇到了同样的问题。每次我打开一个文件时,它都会将 EOL 设置为CRLF(即使我按照人们的建议明确设置了配置lf)。看来问题是我用错误的 Git 配置克隆了我的存储库。这是CRLF默认的。不管怎样,这就是我所做的,而且效果非常好。我的工作区中不再有CRLF。
lf使用以下命令设置 Git 配置git config --global core.autocrlf falsegit clone ...现有的两个答案都有帮助,但不是我需要的。我想将工作区中的所有换行符从 CRLF 批量转换为 LF。
我做了一个简单的扩展来做到这一点
其实这里是扩展代码供参考
'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 次 |
| 最近记录: |