Stop Visual Studio from complaining about inconsistent line endings when working with Unity .cs files

If you are like me, you probably don’t like the constant popup dialogs from Visual Studio and MonoDevelop on the inconsistent line endings. You may not have any idea what I’m talking about but constantly see this in Visual Studio while working with your Unity .cs files

First – what on earth are inconsistent line endings? Let’s look at what happens when you press ENTER on a PC based system vs a Unix based OS like OSX. A PC writes a CR/LF (carriage return / line feed) pair. A LF is ‘n’ or if you like hex 0x0A or 10 in decimal. A CR is ‘r’, 0x0D, 13 respectively.  For you c/c++/c# programmers out there you’ve probably seen some string “Hello Worldn” or “Hello Worldrn” – also noting we have a LF and CR/LF

Let’s take the following text….

See cat run
See sharp

Specifically if you look in a Hex Editor, the bytes in the file look like this on PC (note the highlight 0D 0A)

and like this on a Mac (note the missing 0D)











Why does this happen with Unity and Visual Studio? When Unity creates a C# file, it creates it by default with LF only as shown here.

Visual Studio will open this up without a problem and without notifying you of anything. You aren’t mixing CR/LF with your LF. Now you go ahead and paste some code into here, or use some existing code snippets, or format the document, or rely on some auto-complete/formatting features and they may stick in CR/LF into this doc (as you are editing on a pc).  Now you have a file with LF and CR/LF mixed in it. It’s inconsistent, hence you get a dialog box like the first image shown above Visual Studio loads that file.

Also note that if you do tell Visual Studio to normalize the line endings (ie clicking yes in that first dialog) it will fix this issue, but for this file only.

We have another product called Visual Studio Code (the free cross platform code editor/debugger) which actually just has a little drop down to select line endings and doesn’t prompt you either as its a cross platform code editor. Visual Studio (a very different product) on the other hand is a PC based only full-featured IDE and expects PC based line endings but of course _can_ work with either CR/LF, you just have to tell it not to warn you. It thinks its doing you a favor (some third party tools over the years actually get messed up by mixed line endings, so its generally a good idea to have consistent line endings in your project).

The fix

Aha. At last. The big reveal. The big secret orphan annie ovaltine decoder ring thingy reveal (our ending is a bit better but for those that have no idea what I’m talking about check out this video clip here). The fix is actually super simple. Visual Studio has a feature called Quick Launch in the upper right hand corner. Simply search for “line” as shown below, select “Environment->Documents (Change line ending settings)” and UNCHECK “Check for consistent line endings on load”. Voila. Annoyance resolved. Both the Mono and .NET C# compilers will handle mixed line endings just fine, so there’s no problem here with doing this. Btw – if you are using a PC and aren’t using my free “Right click to open folder as Unity project” registry script, check out my blog post on this. Enjoy!


Source: Adam Tuliper

Leave a Reply

Your email address will not be published.


*