How to make all line endings (EOLs) in all files in Visual Studio Code, UNIX like?

时间秒杀一切 提交于 2020-06-10 07:22:11

问题


I use Windows 10 home and I usually use Visual Studio Code (VSCODE) 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 VSCODE?


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 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 Window unique.


回答1:


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




回答2:


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.




回答3:


To convert the line ending for existing files

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



回答4:


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

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

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

            }
        }

    });

}


来源:https://stackoverflow.com/questions/48692741/how-to-make-all-line-endings-eols-in-all-files-in-visual-studio-code-unix-lik

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!