Is there a way to set EOL to LF in Visual Studio for whole solution or project?
Asked Answered
O

1

9

In Visual Studio you can set file's EOL to CRLF, LF, or CR, however you can do it only to a single file. I'm looking for a way that will make sure that every new file made in my solution will automatically use LF. Any ideas? Thanks in advance!

Overnight answered 30/8, 2020 at 10:28 Comment(5)
Good move! A simple way to get this behavior is to move from this legacy system to linux or OS/X :)Swenson
Open the file for output in binary mode. Or do you mean the source code editor?Castiron
@chqrlie, I use a modern system and it doesn't have a problem with EOL. Use of LF is a design choice.Overnight
@Wispy: my comment was a humorous tease. The CRLF convention, copied from CP/M for MS/DOS in 1981 was already obsolescent then. Apple gave up on the CR convention in 2001. Errare humanum est, perseverare diabolicumSwenson
@chqrlie, I've done just that actually (moved from the legacy system)Overnight
S
10

Adding an .editorconfig file in the root directory of the project (or solution) with the following entry should set line endings to LF by default (for newly added lines, see the notes below).

    [*]
    end_of_line = lf

Quoting from the Create portable, custom editor settings with EditorConfig page.

You can add an EditorConfig file to your project or codebase to enforce consistent coding styles for everyone that works in the codebase.

The editor in Visual Studio supports the core set of EditorConfig properties:
[...]   end_of_line

When you add an .editorconfig file to a folder in your file hierarchy, its settings apply to all applicable files at that level and below.

When you add an EditorConfig file to your project in Visual Studio, new lines of code are formatted according to the EditorConfig settings. The formatting of existing code is not changed unless you run one of the following commands:

  • Code Cleanup (Ctrl+K, Ctrl+E), which applies any white space settings, such as indent style, and selected code style settings, such as how to sort using directives.

  • Edit > Advanced > Format Document (or Ctrl+K, Ctrl+D in the default profile), which only applies white space settings, such as indent style. Note

Sapphism answered 30/8, 2020 at 21:21 Comment(3)
Thank you for the answer, this looks like exactly what I was looking for!Overnight
Note- working on a Unity project... I found my .editorconfig in the Assets folderApgar
@Apgar Thanks. I am not familiar with Unity projects, but going by the documentation that would mean the settings in the .editorconfig you found apply to files under the Assets folder, only. Text files elsewhere in the project would still use the defaults. Adding an .editorconfig file in the root directory of the project would apply to all files under it, both under Assets and elsewhere.Sapphism

© 2022 - 2024 — McMap. All rights reserved.