Configure Visual Studio to use UNIX line endings
Asked Answered
S

10

103

We would like to use Visual Studio 2005 to work on a local copy of an SVN repository. This local copy has been checked out by Mac OS X (and updates and commits will only be made under Mac OS X, so no problem there), and as a consequence the line endings are UNIX-style.

We fear that Visual Studio will introduce Windows-style line endings. Is it possible to force Visual Studio to use UNIX line endings?

Spittoon answered 27/9, 2010 at 9:14 Comment(0)
S
56

Warning: This solution no longer works for Visual Studio 2017 and later. Instead, both of the answers by jcox and Munther Jaber are needed. I have combined them into one answer.

As OP states "File > Advanced Save Options", select Unix Line Endings.

This will only affect new files that are created. Fixing any that were previously created can be done file-by-file or you can search for tools that will fix on-bulk.

Scintillation answered 16/5, 2012 at 4:22 Comment(13)
File > Advanced Save Options is not found on VS2010 express, any alternatives?Vancouver
That seems to change it only for the currently opened file, right?Brittne
It will change the default for newly created files. It will not modify already created files that have consistent line endings. By default Visual Studio will warn you when you open a file that has mixed line endings and prompt you to chose a consistent line ending.Scintillation
@xiao啸 try to set you environment to general msdn.microsoft.com/en-us/library/dd409797.aspxChrischrism
Note if you cannot see this option you need to import environment settings (Tools - > Import and Export Settings) C# settings worked for me.Gamb
This is not a scalable approach, one does not simply "Advanced Save Options" 300 source files every day. It surprise me MS designed Visual Studio that way.Alluvium
@GameDeveloper as per my previous comment changing the setting will apply for all new files created within Visual Studio. To bulk change existing files a different tool is needed - many VCS have automated scripts to check or modify source files on check in.Scintillation
@GameDeveloper I'm not surprised at all. MS has not even the classical gutter (80 chars) for development, you have to install your own plugin, not CTRL-click to show all usages of something, saving in a special encoding is a non-sense from a normal human point of view, and I have many non-sense thing like that for MS. It's totally counter-productive. I have to use this "editor" for Unity, and after using PyCharm and PhpStorm, I feel like I go back to prehistoric age.Alkalinize
@Alluvium This refers to configuring the way new files are created - so it only needs to be done once, not once per file or once per day.Scintillation
This answer is completely out of date. I've been a member of StackOverflow for 10 years and I'm not sure what we should do. Downvoting the answer doesn't seem ... right .. because it removes karma for a once-valid answer.Butcher
If you do not see the "Advanced Save Option" and you are running VS 2017, 2019 or 2022, then checkout the note at the bottom of this MS help page. learn.microsoft.com/en-us/visualstudio/ide/…Taper
@Alluvium • I wish my project only had 300 source files. We have just a skosh under 500,000 source files.Buyers
There is no such option in Visual Studio 2019 CommunityHominoid
B
61

So I found this problem and the answers very confusing, especially since I primarily develop in Linux and PhpStorm and simply never have these issues.

I found that for Visual Studio 2019, only Munther Jaber's solution worked when combined with @jcox's .editorconfig changes for new files.

However, being unfamiliar with Visual Studio itself, it took me a LONG TIME to find out where the line ending box is...

SO I made a video of how to configure this. https://youtu.be/YfN3igHXTPo

Update: I learned how to make animated GIFs via FFMPEG just so I could embed the video here! enter image description here

Butcher answered 27/7, 2020 at 5:40 Comment(5)
I cannot see the bottom right buttons in that bar, how do you activate those?Quicken
Make sure you are using Visual Studio and NOT Visual Studio Code. That's a major point of confusion in both questions and answers...Butcher
Your Youtube video has changed my life in just 31 seconds. I will say, to avoid other users being confused, the gif misses the crucial step of creating an .editorconfig file!Chretien
If you don't see the "line endings" toggle shown in the picture and you are using Visual Studio, enable it by going to Tools / Options / Text Editor / General. It's under 'Display' at the very bottom, along with toggles for line/col, selections, insert/overwrite, and tab/space.Crystallite
That line-ending editor feature works only on an individual file. As a better solution for all files in a project or solution, use the .editorconfig file as described in @jcox's answer.Marcellusmarcelo
E
57

Here are some options available for Visual Studio Community 2017

  1. "File > Advanced Save Options" has been removed by microsoft due to "uncommon use". Whatever that means. https://developercommunity.visualstudio.com/content/problem/8290/file-advanced-save-options-option-is-missed.html You can add it back by going to "Tools>Customize", then "Commands" tab, select the drop down next to "Menu Bar" select "File" then "Add Command">File>Advanced Save Options..". You can then reorder it in the file menu by using "move down".

I don't know if you will have to then set the advanced save options for each and every file, but it might prevent the issue I was having where my Visual Studio kept adding CL RF line endings into my files that were uniformly LF.

  1. But I took it one step further and I added an extension called "Line Endings Unifier" by going to "Tools>Extensions and Updates>Online" and then searching for "line endings" in the search bar to the right. I will use this to automatically force all of my scripts to save with uniform line endings of my choice, but you can do more with it. https://marketplace.visualstudio.com/items?itemName=JakubBielawa.LineEndingsUnifier

  2. strip'em is another solution that does something similar to Line Endings Unifier. http://www.grebulon.com/software/stripem.php

I am not sure how they differ or the advantages/disadvantages of either. I'm mainly using Line Endings Unifier just because it was in the Visual Studio Marketplace. I think I've used all of these methods in the past, but my memory is fuzzy.

Eogene answered 15/11, 2017 at 23:1 Comment(3)
As of July 2018 there is another option - Choose File -> Save As then on the save dialog the save button has a dropdown for save with encoding. You can set the line endings there.Septivalent
@Septivalent I tested this, and confirms that it does work in visual studio community 2017 version 15.7.4 According to Zero, when using advanced save options, "changing the setting will apply for all new files created within Visual Studio". I wonder if this is true for this method as well.Eogene
Thank you for LineEndingsUnifier. Would you be so kind as to update it for VS2022?Barred
S
56

Warning: This solution no longer works for Visual Studio 2017 and later. Instead, both of the answers by jcox and Munther Jaber are needed. I have combined them into one answer.

As OP states "File > Advanced Save Options", select Unix Line Endings.

This will only affect new files that are created. Fixing any that were previously created can be done file-by-file or you can search for tools that will fix on-bulk.

Scintillation answered 16/5, 2012 at 4:22 Comment(13)
File > Advanced Save Options is not found on VS2010 express, any alternatives?Vancouver
That seems to change it only for the currently opened file, right?Brittne
It will change the default for newly created files. It will not modify already created files that have consistent line endings. By default Visual Studio will warn you when you open a file that has mixed line endings and prompt you to chose a consistent line ending.Scintillation
@xiao啸 try to set you environment to general msdn.microsoft.com/en-us/library/dd409797.aspxChrischrism
Note if you cannot see this option you need to import environment settings (Tools - > Import and Export Settings) C# settings worked for me.Gamb
This is not a scalable approach, one does not simply "Advanced Save Options" 300 source files every day. It surprise me MS designed Visual Studio that way.Alluvium
@GameDeveloper as per my previous comment changing the setting will apply for all new files created within Visual Studio. To bulk change existing files a different tool is needed - many VCS have automated scripts to check or modify source files on check in.Scintillation
@GameDeveloper I'm not surprised at all. MS has not even the classical gutter (80 chars) for development, you have to install your own plugin, not CTRL-click to show all usages of something, saving in a special encoding is a non-sense from a normal human point of view, and I have many non-sense thing like that for MS. It's totally counter-productive. I have to use this "editor" for Unity, and after using PyCharm and PhpStorm, I feel like I go back to prehistoric age.Alkalinize
@Alluvium This refers to configuring the way new files are created - so it only needs to be done once, not once per file or once per day.Scintillation
This answer is completely out of date. I've been a member of StackOverflow for 10 years and I'm not sure what we should do. Downvoting the answer doesn't seem ... right .. because it removes karma for a once-valid answer.Butcher
If you do not see the "Advanced Save Option" and you are running VS 2017, 2019 or 2022, then checkout the note at the bottom of this MS help page. learn.microsoft.com/en-us/visualstudio/ide/…Taper
@Alluvium • I wish my project only had 300 source files. We have just a skosh under 500,000 source files.Buyers
There is no such option in Visual Studio 2019 CommunityHominoid
R
36

VS2017 supports .editorconfig files, which can be checked in along with your project. This is the relevant command for setting Unix line endings:

end_of_line = lf
Resurge answered 28/6, 2018 at 20:17 Comment(3)
This only works for new files for me. I included it in my answer, tho. #3802906Butcher
Note- working on a Unity project... I found my .editorconfig in the Assets folderArteaga
worked for my VS 2019 Unity project. Didn't have to install any additional pluginsNewton
C
10

"File > Advanced Save Options", select Unix Line Endings works per file based.

Alternatively You can use Strip'em Add-in for Visual Studio

This Add-in converts the text format of a file when it is saved in Visual Studio.

More info here: http://www.grebulon.com/software/stripem.php

Chrischrism answered 4/3, 2015 at 14:5 Comment(0)
I
6

There are some VS extension that are useful to solve this problem I will recommend Trim line ends on save. This extension is free and open source. There are similar extensions that may work for you, just go to TOOLS | Extensions and Updates and search online for "line end"

Interrelated answered 15/4, 2015 at 19:14 Comment(1)
More valuable to me than the answers that refer to Advanced Save Options, though I decided to go with a different extension (Line Endings Unifier).Abaca
M
5

For Visual Studio 2019 for Mac

Go to Project> Solution Options> Source Code> Code Formatting> Text File In the Line Terminations drop box select Unix/Mac.

Image

Minh answered 4/6, 2020 at 9:3 Comment(0)
F
4

enter image description here The LF is clickable and you choose either LF or CRLF.

Furry answered 15/8, 2018 at 20:44 Comment(3)
Pretty sure that visual studio code, not visual studio.Resurge
For those using VS 2019 (I'm unsure about older versions) there is an option just like this in the bottom right of the text editor, which is easier imo than using "advanced save options."Revealment
Here's a video for everyone who was as confused as me: #3802906Butcher
B
1

It is possible to force line endings in Visual Studio 2017 for Mac

  1. Go to Tools > Add Custom Tool....
  2. In the dialog box that appears scroll the left menu down to the Text Editor section and select General.
  3. In the first option, Line ending conversion, change Leave line endings as is to Always convert line endings.

screenshot of dialog

Betweentimes answered 21/12, 2018 at 2:39 Comment(0)
E
0

In the latest version on Windows you go to

File > Preferences > Settings > Text Editor > Files > Eol
Eccentric answered 12/11, 2021 at 20:40 Comment(1)
Simple answer for newer VSCode 1.87.1Noles

© 2022 - 2024 — McMap. All rights reserved.