Stop Visual Studio from mixing line endings in files

Visual StudioVisual Studio-2010Newline

Visual Studio Problem Overview


When opening a text based file in Visual Studio 2010 it will then write my edits with CRLF instead of the line ending format of the original file. How can I stop VS from doing this? Any half decent editor should have this capability.

What's worse is that since VS wrote the file with portions in CRLF, it then (when opening the file again) will present a dialog asking me to convert the files line ending.

Visual Studio Solutions


Solution 1 - Visual Studio

On the File menu, choose Advanced Save Options, you can control it there.

Edit: Here's the documentation, you should have a file open first.

Solution 2 - Visual Studio

In Visual Studio 2015 (this still holds in 2019 for the same value), check the setting:

Tools > Options > Environment > Documents > Check for consistent line endings on load

VS2015 will now prompt you to convert line endings when you open a file where they are inconsistent, so all you need to do is open the files, select the desired option from the prompt and save them again.

Solution 3 - Visual Studio

With VS2010+ there is a plugin solution: Line Endings Unifier.

With the plugin installed you can right click files and folders in the solution explorer and invoke the menu item Unify Line Endings in this file

Configuration for this is available via

> Tools -> Options -> Line Endings Unifier.

The default file extension list that is included is pretty narrow:

 .cpp; .c; .h; .hpp; .cs; .js; .vb; .txt;

Might want to use something like:

 .cpp; .c; .h; .hpp; .cs; .js; .vb; .txt; .scss; .coffee; .ts; .jsx; .markdown; .config

Solution 4 - Visual Studio

see http://editorconfig.org and https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2017

  1. If it does not exist, add a new file called .editorconfig for your project

  2. manipulate editor config to use your preferred behaviour.

I prefer spaces over tabs, and CRLF for all code files.
Here's my .editorconfig

# http://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = crlf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

[*.tmpl.html]
indent_size = 4

[*.scss]
indent_size = 2 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrett RyanView Question on Stackoverflow
Solution 1 - Visual StudioGvSView Answer on Stackoverflow
Solution 2 - Visual StudioJohann CaronView Answer on Stackoverflow
Solution 3 - Visual StudiogearsinView Answer on Stackoverflow
Solution 4 - Visual StudioChris SchallerView Answer on Stackoverflow