Eclipse 和 Windows 换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1886185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eclipse and Windows newlines
提问by Vasu
I had to move my Eclipse workspace from Linux to Windows when my desktop crashed. A week later I copy it back to Linux, code happily, commit to CVS. And alas, windows newlines have polluted many files, so CVS diff dumps the entire file, even when I changed a line or two!
当我的桌面崩溃时,我不得不将我的 Eclipse 工作区从 Linux 移动到 Windows。一周后,我将它复制回 Linux,愉快地编写代码,提交到 CVS。唉,windows 换行符污染了许多文件,所以 CVS diff 会转储整个文件,即使我更改了一两行!
I could cook up a script, but I am wondering if it will mess up my Eclipse project files.
我可以编写一个脚本,但我想知道它是否会弄乱我的 Eclipse 项目文件。
回答by VonC
Set file encoding to
UTF-8
and line-endings for new files to Unix, so that text files are saved in a format that is not specific to the Windows OS and most easily shared across heterogeneous developer desktops:
- Navigate to the Workspace preferences (General:Workspace)
- Change the Text File Encoding to
UTF-8
- Change the New Text File Line Delimiter to Other and choose Unix from the pick-list
将新文件的文件编码设置
UTF-8
为 Unix,并将新文件的行尾设置为 Unix,以便文本文件以不特定于 Windows 操作系统的格式保存,并且最容易在异构开发人员桌面之间共享:
- 导航到工作区首选项 (General:Workspace)
- 将文本文件编码更改为
UTF-8
- 将新文本文件行分隔符更改为其他并从选择列表中选择 Unix
- Note: to convert the line endings of an existing file, open the file in Eclipse and choose
File : Convert Line Delimiters to : Unix
- 注意:要转换现有文件的行尾,请在 Eclipse 中打开文件并选择
File : Convert Line Delimiters to : Unix
Tip: You can easily convert existing file by selecting then in the Package Explorer, and then going to the menu entry File : Convert Line Delimiters to : Unix
提示:您可以通过在 Package Explorer 中选择 then 来轻松转换现有文件,然后转到菜单项File : Convert Line Delimiters to : Unix
回答by user988282
回答by Ben Hayden
There is a handy bash utility - dos2unix
- which is a DOS/MAC to UNIX text file format converter, that if not already installed on your distro, should be able to be easily installed via a package manager. dos2unix man page
有一个方便的 bash 实用程序——dos2unix
它是一个 DOS/MAC 到 UNIX 文本文件格式的转换器,如果你的发行版上还没有安装它,应该能够通过包管理器轻松安装。 dos2unix 手册页
回答by Michael Scheper
In addition to the Eclipse solutions and the tool mentioned in another answer, consider flip. It can 'flip' either way between normal and Windows linebreaks, and does nice things like preserve the file's timestamp and other stats.
除了 Eclipse 解决方案和另一个答案中提到的工具,请考虑flip。它可以在普通换行符和 Windows 换行符之间以任何一种方式“翻转”,并且可以做一些不错的事情,例如保留文件的时间戳和其他统计信息。
You can use it like this to solve your problem:
您可以像这样使用它来解决您的问题:
find . -type f -not -path './.git/*' -exec flip -u {} \;
(I put in a clause to ignore your .git directory, in case you use git, but since flip ignores binary files by default, you mightn't need this.)
(我添加了一个子句来忽略您的 .git 目录,以防您使用 git,但由于默认情况下 flip 忽略二进制文件,您可能不需要这个。)
回答by Ham Vocke
You could give it a try. The problem is that Windows inserts a carriage return as well as a line feed when given a new line. Unix-systems just insert a line feed. So the extra carriage return character could be the reason why your eclipse messes up with the newlines.
你可以试一试。问题在于,当给定新行时,Windows 会插入回车符和换行符。Unix 系统只是插入一个换行符。因此,额外的回车符可能是您的日食与换行符混淆的原因。
Grab one or two files from your project and convert them. You could use Notepad++ to do so. Just open the file, go to Format->Convert to Unix (when you are using windows).
从您的项目中获取一两个文件并进行转换。您可以使用 Notepad++ 来执行此操作。只需打开文件,转到“格式”->“转换为 Unix”(使用 Windows 时)。
In Linux just try this on a command line:
在 Linux 中,只需在命令行上试试这个:
sed 's/$'"/`echo \\r`/" yourfile.java > output.java
回答by Gabe
To recursively remove the carriage returns (\r
) from the CVS/* files in all child directories, run the following in a unix shell:
要从\r
所有子目录中的 CVS/* 文件中递归删除回车符 ( ),请在 unix shell 中运行以下命令:
find ./ -wholename "\*CVS/[RE]\*" -exec dos2unix -q -o {} \;