Changing file EOL with vscode extension API
Asked Answered
H

3

8

Can I silently change end of line sequence in VSCode?
Something like this:

vscode.commands.executeCommand("workbench.action.editor.changeEOL", "LF");
Hefter answered 19/9, 2018 at 10:44 Comment(0)
H
1

The solution is to call edit method of active TextEditor. For example:

vscode.window.activeTextEditor.edit(builder => { 
    builder.setEndOfLine(vscode.EndOfLine.LF);
})

UPD
Or if you want TexEdit result you can use this one:

vscode.TextEdit.setEndOfLine(vscode.EndOfLine.LF)

"vscode" here is vscode module: require('vscode')

Hefter answered 5/12, 2018 at 20:12 Comment(5)
Is this a script snippet ? How to execute it ?Bowden
@Alan, you need to call edit method of TextEditor object. For example: require('vscode').window.activeTextEditor.edit(builder => { builder.setEndOfLine(vscode.EndOfLine.LF); })Hefter
Although this is the accepted answer, it would be improved with simpler instructions within the body and not a comment. Specifically how would one "call the edit method" for example.Wharve
It might be the solution but it should contain the instructions as to where and how a person should execute the code. Poorly answered.Hygro
This question is about VSCode extension development. I produced a code, that solves the problem. Where and how execute the code (of extension you're working at) is not the instruction for this question i hope)Hefter
Y
13

You can add this line to your user preferences settings (CTRL + ,):

"files.eol": "\n"
Yentai answered 30/11, 2018 at 17:54 Comment(0)
C
5

On the bottom right of vs code it will say lf or crlf. Click there and it will give an option to change.

pic of vs code

Crosspollination answered 20/11, 2018 at 23:40 Comment(1)
That only applies it to the current file. Is there a way to change it for every file in the project? Just changing it in the Settings does not do this unfortunately.Pshaw
H
1

The solution is to call edit method of active TextEditor. For example:

vscode.window.activeTextEditor.edit(builder => { 
    builder.setEndOfLine(vscode.EndOfLine.LF);
})

UPD
Or if you want TexEdit result you can use this one:

vscode.TextEdit.setEndOfLine(vscode.EndOfLine.LF)

"vscode" here is vscode module: require('vscode')

Hefter answered 5/12, 2018 at 20:12 Comment(5)
Is this a script snippet ? How to execute it ?Bowden
@Alan, you need to call edit method of TextEditor object. For example: require('vscode').window.activeTextEditor.edit(builder => { builder.setEndOfLine(vscode.EndOfLine.LF); })Hefter
Although this is the accepted answer, it would be improved with simpler instructions within the body and not a comment. Specifically how would one "call the edit method" for example.Wharve
It might be the solution but it should contain the instructions as to where and how a person should execute the code. Poorly answered.Hygro
This question is about VSCode extension development. I produced a code, that solves the problem. Where and how execute the code (of extension you're working at) is not the instruction for this question i hope)Hefter

© 2022 - 2024 — McMap. All rights reserved.