193

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!

I could cook up a script, but I am wondering if it will mess up my Eclipse project files.

Line
  • 1,529
  • 3
  • 18
  • 42
Vasu
  • 2,406
  • 3
  • 20
  • 26
  • 1
    For converting line endings for few/all files in a project with just a few clicks: https://stackoverflow.com/a/7019737/1364747 – Teddy Jul 04 '17 at 06:17

6 Answers6

373

As mentioned here and here:

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

alt text

  • Note: to convert the line endings of an existing file, open the file in Eclipse and choose 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

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    Thats exactly what I was looking for! I had already changed it for new files, but the option to convert - missed my eyes. Thanks VonC! – Vasu Dec 12 '09 at 12:47
  • @Vasu: you're welcome :). The Outline view and Package Explorer contain some features for multi-selected elements which are often overlooked. – VonC Dec 12 '09 at 13:03
  • Great writeup. Searching for this myself through Eclipse's horribly labyrinthian preferences dialog was driving me insane. – Cerin Jun 13 '11 at 01:01
  • 41
    If you choose an entire project in package explorer you can use the `File : Convert Line Delimiters to : Unix` command to "fix" all files in the project at once. – Tal Weiss Nov 01 '12 at 22:03
13

I had the same, eclipse polluted files even with one line change. Solution: Eclipse git settings -> Add Entry: Key: core.autocrlf Values: true

enter image description here

enter image description here

user988282
  • 166
  • 1
  • 4
  • While this is an OK solution for most cases, it can bite you - for example when you're calculating checksums over source files. These will then differ from platform to platform. – zb226 Apr 06 '20 at 10:35
8

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

Ben Hayden
  • 1,349
  • 9
  • 15
  • I was planning to write a script to recursively do this over my eclipse workspace. Only worry was corrupting any eclipse internal project files. Thanks for your suggestion! – Vasu Dec 12 '09 at 12:55
7

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.

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

Community
  • 1
  • 1
Michael Scheper
  • 6,514
  • 7
  • 63
  • 76
  • Just a note... this command is for Linux users, not Windows users. Windows `find` does not work this way. – Brad May 31 '15 at 00:14
  • @Brad is correct. However, there exists a Cygwin port of flip: https://github.com/jaalto/cygwin-package--flip/ – Michael Scheper May 31 '15 at 00:38
3

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.

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

In Linux just try this on a command line:

sed 's/$'"/`echo \\\r`/" yourfile.java > output.java
Ham Vocke
  • 2,942
  • 22
  • 27
  • I have around 10k files! So had to automate it somehow. Thanks for the advice! – Vasu Dec 12 '09 at 13:00
  • Some typo error on your CLI: ``sed -i 's/$`echo \\\r`//' yourfile.java`` and to automate it ``find . -name "*.java" -exec sed -i 's/$`echo \\\r`//' {} \; `` – pdem Mar 07 '18 at 13:04
3

To recursively remove the carriage returns (\r) from the CVS/* files in all child directories, run the following in a unix shell:

find ./ -wholename "\*CVS/[RE]\*" -exec dos2unix -q -o {} \;
Gabe
  • 31
  • 2