138

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.

Brett Ryan
  • 23,468
  • 28
  • 117
  • 152
  • 8
    You may want to vote for this: http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/4160768-save-text-files-with-the-newline-convention-presen – Jon Watte Jul 05 '13 at 17:36
  • 1
    @JonWatte currently under review as of April 28, 2016 – Chris Marisic May 25 '16 at 17:44
  • 2
    I've moved away from .NET these days, got too fed up with MS seemingly changing focus every season and neglecting simple issues such as these. Glad to see them finally fixing it. – Brett Ryan May 26 '16 at 20:50
  • Try this add-in: [Strip'em](http://grebulon.com/software/stripem.php) – makes Nov 10 '11 at 11:55

4 Answers4

85

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.

GvS
  • 50,659
  • 16
  • 96
  • 138
  • 6
    I don't have this menu item on my VS2010 ultimate install. – Palantir Oct 22 '10 at 08:16
  • 6
    I do have this setting in VS2010 Ultimate, however this option is not a global persistent option and is only valid for the current editor session. All I want is for VS to respect the currently open files line-endings when editing the file. – Brett Ryan Oct 25 '10 at 01:26
  • 2
    File + Save As, click on the arrow on the Save button. Same thing but specific to the file. – Hans Passant Nov 10 '11 at 12:18
  • 14
    For those that don't see the command in the `File` menu, you may need to go to `Tools`, `Customize`, `Commands` tab, `Menu Bar`: `File`, `Add Command...`, `File`, `Advanced Save Options...`, `Ok`, `Close`. Now you should have the option. – davidg Mar 31 '13 at 00:48
  • 98
    This doesn't actually solve the problem, because you have to choose that option EVERY TIME YOU SAVE. It also means that you have to CHOOSE A LINE ENDING OPTION every time you save. The right behavior, which every other text editor out there supports, is to detect the format on load, and save in that format on save. There is no way to get VS2010 to do that AFAICT, and that's a shame. – Jon Watte Jul 05 '13 at 17:26
78

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.

jcolebrand
  • 15,923
  • 10
  • 71
  • 117
Johann Caron
  • 813
  • 6
  • 3
  • 4
    While this might be helpful now, note that the question was tagged [tag:visual-studio-2010]. – dakab Nov 06 '15 at 08:16
  • 4
    The problem with this option is that it does not tell you which line endings are the most common in the document, so I have to open the document in Notepad++ first and then only select the correct option. Besides this is a saving problem, not a loading one. – Strategy Thinker Dec 27 '15 at 17:52
  • 4
    The problem I was having is that I was opening files, with LF endings, and then every line I edited or added would saved with CR LF endings. When I opened the file again, it would ask me what I wanted to changed the inconsistent line endings to and I would choose LF and then the process would repeat. here is how I solved it: https://stackoverflow.com/a/47318778/2701911 – FragmentalStew Nov 15 '17 at 23:05
  • That does not resolve the issue on copy-pasting a text with mixed line endings. You have to reopen the file to trigger the check. – Andry Feb 08 '19 at 12:47
24

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
Chris Marisic
  • 30,638
  • 21
  • 158
  • 255
gearsin
  • 439
  • 1
  • 4
  • 7
  • 11
    This is actually a [plugin](https://visualstudiogallery.msdn.microsoft.com/b2bbadd2-c337-43d7-9343-752ebbdd900f) and not installed by default. – null Oct 09 '15 at 06:59
  • 1
    @null thanks for pointing out that, this is actually what i was looking for. I updated the answer to better reflect all of this – Chris Marisic May 25 '16 at 17:44
20

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 
Chris Schaller
  • 6,580
  • 3
  • 34
  • 56