pátek 10. dubna 2020

Unix Line Endings in Visual Studio

In the current world of cross-platform .NET Core development you might want to use Unix-style line endings in your code-base. Visual Studio uses Windows-style by default. In this article I'm going to explain how to change that.

On Windows machines ends of lines are encoded using a pair of characters '\r\n' (also known as CRLF, which is a shortcut for carriage return + line feed and resembles old typewriters). On Unix a single character '\n' (or LF) is used.

In Visual Studio 2019 you can switch line endings at the bottom of the editor window, as depicted on the following screenshot. This is tracked for each file separately and gets reset with every file modification, which is not very convenient.

Editor Config

It's better to specify line endings globally for the whole solution, which can be done using Editor Config. To set it up follow these simple steps. If you already have an .editorconfig file in your solution, you can skip steps 1 - 4.

1. Right click on your solution in Solution Explorer.
2. Select Add > New Item.
3. Choose editorconfig File (default).
4. Click Add.

This will create a new file named .editorconfig in your solution folder, which gets automatically opened in your Visual Studio editor window.


5. Replace the file contents with the following and save.

[*]
end_of_line = "lf"

From now on, Visual Studio will use Unix line endings for all files in your solution. Note however, that existing files will not get automatically converted.

For more information visit Editor Config documentation at
https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2019