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

Viewed 2513

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!

3 Answers

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

If you are looking for a way to control the default translation mode for opening files in Windows, you can use _set_mode() or set the _fmode global variable documented here:

    int _fmode = _O_BINARY;

Or at run time:

    _set_mode(_O_BINARY);  // defined in `<stdlib.h>`

There seems to be a way to set this more globally by modifying the Global state in the CRT, but the details are beyond my skill level in this environment.

Because default settings.json contain "files.eol": "auto", you can browse this under VSCode documentation. You need set The default end of line character to appropriate value, e.g.: enter image description here

Related