Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch normalize line endings in Visual Studio

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:

  • What does Visual Studio mean by normalize inconsistent line endings?
  • https://stackoverflow.com/questions/2868021/visual-studio-2010-and-line-endings
  • Normalizing line endings in Visual Studio 2010
like image 398
Borek Bernard Avatar asked Feb 20 '12 18:02

Borek Bernard


1 Answers

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 :)

like image 187
plgod Avatar answered Oct 08 '22 08:10

plgod