windows 如何在 Visual Studio Code、UNIX 之类的所有文件中制作所有行结尾 (EOL)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48692741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make all line endings (EOLs) in all files in Visual Studio Code, UNIX like?
提问by user9303970
I use Windows 10 home and I usually use Visual Studio Code (VSCODE) to edit Linux Bash scripts as well as PHP and JavaScript.
我使用 Windows 10 家庭版,我通常使用 Visual Studio Code (VSCODE) 来编辑 Linux Bash 脚本以及 PHP 和 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).
我没有为 Windows 开发任何专门的东西,我不介意我编辑的所有文件的默认 EOL 将是 Unix 之类的 (nix)。
How could I ensure that all EOLs, in all files whatsoever (from whatever file extension), are nix, in VSCODE?
在 VSCODE 中,我如何确保所有文件(无论文件扩展名)中的所有 EOL 都是 nix?
I ask this question after I've written a few Bash scripts in Windows with VSCODE, uploaded them to GitHub as part of a project, and a senior programmer that reviewed the project told me I have Windows EOLsthere and also a BOMproblem that I could solve if I'll change the EOLs there to be nix (or that's what I understood, at least).
在我用 VSCODE 在 Windows 中编写了一些 Bash 脚本,将它们作为项目的一部分上传到 GitHub 之后,我问了这个问题,一位该项目的高级程序员告诉我,我在那里有Windows EOL,还有一个BOM问题,我如果我将那里的 EOL 更改为 nix(或者至少我是这么理解的),可以解决这个问题。
Because all my development is Linux-oriented, I would prefer that by default, anyfile I edit would have nix EOLs, even if it's Window unique.
因为我所有的开发都是面向 Linux 的,所以我更喜欢默认情况下,我编辑的任何文件都会有 nix EOL,即使它是 Window 唯一的。
采纳答案by Mike
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.
这是在提交639a3cb时添加的,因此您显然需要在该提交之后使用一个版本。
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.
注意:即使CRLF
文件中有一个,上面的设置也将被忽略,整个文件将被转换为CRLF
. 您首先需要将所有内容转换CRLF
为LF
在 Visual Studio Code 中打开它。
回答by Ian McGowan
I was having the same problem - editing files on windows usually destined for a Unix server (using the awesome ftp-sync plugin) and almost always want LF line endings. It took an embarrassingly long time for me to notice the current setting in the bottom right, and if you click on it you can toggle the setting for just the current file.
我遇到了同样的问题 - 在 Windows 上编辑文件通常是用于 Unix 服务器(使用很棒的 ftp-sync 插件)并且几乎总是想要 LF 行结尾。我花了很长时间才注意到右下角的当前设置,如果你点击它,你可以只为当前文件切换设置。
回答by JesusIniesta
To convert the line ending for existing files
转换现有文件的行尾
We can use dos2unixin WSLor in your Shell terminal.
我们可以在WSL或您的 Shell 终端中使用dos2unix。
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
回答by Jim W says reinstate Monica
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.
现有的两个答案都有帮助,但不是我需要的。我想将工作区中的所有换行符从 CRLF 批量转换为 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");
}
}
});
}