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:
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:
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 :)
You can create a bat file that normalize all files recursively.
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.
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.
I just used Notepad++ find and replace feature :
you should back up or commit any changes you have before doing this