Switching EOL Styles

There are many things in programming that can p*ss you off. Simple things like line endings in the wrong (or mixed) format are even more irritating.

For some unknown reason, occasionally my PHP IDE decides, by itself, to start converting my unix line endings to Windows ones. Yes, I do have the correct configuration setup. So, while this bug in the IDE exists, I like to run a quick command in my Ubuntu shell before commiting the code back into the repository.

First, you need a little app called flip (available for multiple platforms but I’ll focus on Ubuntu).

sudo apt-get install flip

Next, head to your project root folder and run the following command:

for i in `find . | grep -v "\.svn"`; do flip -vu $i; done;

This will first find all non-Subversion files then flip them to Unix-format line endings. And don’t worry about binary files, these will be untouched unless you explicitly tell flip to convert these using the -b option.

On Solaris and other linux flavours, you will probably have access to dos2unix which does the same thing. The syntax is a little different:

dos2unix <filename> <filename>

That’s not a typo, specify the filename twice otherwise you’ll get the updated content spewed out at you in STDOUT.

3 Comments to “Switching EOL Styles”

  1. John Wright 15 May 2009 at 10:41am #

    Also, for any Notepad++ users, it has an inbuilt option (within the format menu) to change the encoding to Windows, Mac or Unix formats from a dropdown :)

  2. TomSinger 2 June 2009 at 5:54pm #

    Yes Adam, it “switches itself” we all believe you ;)

    Also not all versions work as
    dos2unix
    some of them just use
    dos2unix

    Who needs consistency?

  3. TomSinger 2 June 2009 at 5:59pm #

    Ok, it screwed up my syntax, what I meant to write was:

    Also not all versions work as
    dos2unix <infile> <outfile>
    some of them just use
    dos2unix <filename>


Leave a Reply

You must be logged in to post a comment.