How to fix inconsistent line endings for whole VS solution?

不羁的心 提交于 2019-12-03 04:43:19

问题


Visual Studio will detect inconsistent line endings when opening a file and there is an option to fix it for that specific file. However, if I want to fix line endings for all files in a solution, how do I do that?


回答1:


Just for a more complete answer, this worked best for me:

Replace

(?<!\r)\n

with

\r\n

in entire solution with "regEx" option.

This will set the correct line ending in all files which didn't have the correct line ending so far. It uses the negative lookahead to check for the non-existance of a \r in front of the \n.

Be careful with the other solutions: They will either modify all lines in all files (ignoring the original line ending) or delete the last character of each line.




回答2:


You can use the Replace in Files command and enable regular expressions. For example, to replace end-of-lines that have a single linefeed "\n" (like, from GitHub, for example) with the standard Windows carriage-return linefeed "\r\n", search for:

([^\r]|^)\n

This says to create a group (that's why the parentheses are required), where the first character is either not a carriage-return or is the beginning of a line. The beginning of the line test is really only for the very beginning of the file, if it happens to start with a "\n". Following the group is a newline. So, you will match ";\n" which has the wrong end-of-line, but not "\r\n" which is the correct end-of-line.

And replace it with:

$1\r\n

This says to keep the group ($1) and then replace the "\n" with "\r\n".




回答3:


Try doing

Edit > Advanced > Format Document

Then save the document, as long as the file doesn't get modified by another external editor, it should stay consistent. Fixes it for me.




回答4:


Update: Please be careful. Don't do this unless you've completely committed to your code repository already and you can roll back any unintentional changes. Read the comments below. Things that worked for me might not work for you.


Ron's answer was really close to helping me, but I had to make some adjustments to make it work in Visual Studio.

1) open the solution you want to fix this in.

2) make sure all pending changes are checked in because you are potentially going to edit a crapton of documents and don't want to undo your other pending changes with this.

3) in any document press Ctrl+F to open the search box, and click the down-arrow icon on the left side to expand it to the search/replace box.

4) In the top line, paste:

([^\r]|^)\n

In the bottom line, paste:

\r\n

5) Enable the .* icon to "Use Regular Expressions" feature.

6) Change the drop down next to that icon so it says "Entire Solution" or "All Open Documents" or whatever is appropriate for your situation.

7) Click the "Replace All" button that shows ab -> ac in the icon.

And that's it!

As an aside, I needed this because Unity's script templates are all made in MonoDevelop so they've got the Mac line endings by default. I had to edit all the documents in the path:

C:\Program Files\Unity\Editor\Data\Resources\ScriptTemplates

So I opened them all in Visual Studio, followed the process I laid out above, using "All Open Documents" in step 6.




回答5:


If you have Cygwin with the cygutils package installed, you can use this chain of commands from the Cygwin shell:

unix2dos -idu *.cpp | sed -e 's/ 0 [1-9][0-9]//' -e 's/ [1-9][0-9]* 0 //' | sed '/ [1-9][0-9] / !d' | sed -e 's/ [1-9][0-9 ] //' | xargs unix2dos

(Replace the *.cpp with whatever wildcard you need)

To understand how this works, the unix2dos command is used to convert the files, but only files that have inconsistent line endings (i.e., a mixture of UNIX and DOS) need to be converted. The -idu option displays the number of dos and unix line endings in the file. For example:

   0     491  Acad2S5kDim.cpp
 689       0  Acad2S5kPolyline.cpp
   0     120  Acad2S5kRaster.cpp
 433      12  Acad2S5kXhat.cpp
   0     115  AppAuditInfo.cpp

Here, only the Acad2S5kXhat.cpp file needs to be converted. The sed commands filter the output to produce a list of just the files that need to be converted, and these are then processed via xargs.



来源:https://stackoverflow.com/questions/9294779/how-to-fix-inconsistent-line-endings-for-whole-vs-solution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!