15

When you open a file with mixed line endings, Visual Studio will prompt you to normalize it. Is there a way to normalize all files in the current solution?

Related posts that don't answer my question:

4 Answers 4

20

As yellowblood pointed out in Aaron F.'s answer, if you replace \n (LF) with \r\n (CRLF), you will have a bad time since it will add a CR before every LF, even those that already had one.

However, what you want can be achieved with regular expressions using any text editor that supports batch replacing in files (like Notepad++ or Visual Studio's "Replace in Files").

For example, to replace LF with CRLF, make sure to activate the regex option then replace all occurences of

(?<!\r)\n 

with

\r\n 

\r is a carriage return (CR), \n is a line feed (LF). The pattern (?<!\r)\n will match any line feed whose previous character is not a carriage return, without capturing (i.e. replacing) that previous character.

The other way around is much simpler: simply replace \r\n with \n.

As always, back up your files and make sure to test the operation on a single file before processing the whole solution.

I would have added this as a comment to Aaron F.'s answer but my reputation isn't high enough :)

Sign up to request clarification or add additional context in comments.

Comments

6

You can create a bat file that normalize all files recursively.

  1. Download the command line tool Tofrodos.
  2. Create a bat file in the root of you solution directory with the following content:

for /R %%i in (*.cs) do "C:\Prgs\Tofrodos\todos.exe" -p "%%i"

Note that you have to update the file path to todos.exe, and possibly the *.cs filter.

Then you run the bat file and wait for it to finish.

Comments

2

Visual Studio doesn't have a mechanism for normalizing line endings for an entire solution. The feature is essentially limited to checking if a file is correct upon opening and changing at that point.

The normalization of the line endings though is an API that is exposed by Visual Studio in IEditorOperations::NormalizeLineEndings. Hence it is possible to write a plugin / script which does the action against items in the solution.

4 Comments

That is nice! I've never written a plugin / script for VS, can I just open up a PowerShell console and write some loop over all solution files?
@Borek unfortunately it's actually not very easy. The main problem you'll run into is that there's not a great definition of what a user controlled file is. For example you probably don't want to do this against say a generated file but it doesn't look much different than a normal file from an API perspective. Also not all files in the IDE have a real physical file backing them on disk (also hard to tell from an API). In general enumerating the files will work but you end up with all of these corner / special cases. In all likelyhood it's why the VS behavior is what it is today
That's a pity, I thought there would be an easy way to get a list of everything I see in a solution explorer (and is a file).
@Borek you can definitely get a list of everything you see by enumerating the list of project items. But actually poking the project item and determining if it's a real user file is tricky. It's a heuristic at best. I wish I had a decent one to throw your way but I avoid the project items for this very reason.
2

I just used Notepad++ find and replace feature :

  1. Set NotePad++ to show line endings, view->show symbols -> show all characters
  2. Open up the find in files dialog
  3. set find what to \r\n (or \n) depending on which coversion you're doing
  4. set replace with to \n (or \r\n)
  5. Point the Directory at your solution folder

you should back up or commit any changes you have before doing this

1 Comment

if you replace \n with \r\n you're gonna have a bad time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.