How to Ensure Always LF not CRLF on Windows

Answered

I am using PhpStorm 2016.2.2 on Windows 10 and want to only ever have Unix line feeds.

This doesn't seem possible, or it's broken, or I've missed some fundamental setting somewhere. 

I followed the documentation to set the line feed to Unix, I used another software (UltraEdit) to change the entire project files to LF Unix line endings, but still, every time I reboot and start working on a new file, I see CRLF in the little selector in the lower right hand corner of the editor window.

What am I missing or doing wrong?

12 comments
Comment actions Permalink

Hi there,

Since I do not know what you have done there -- have a look at this SO answer first:

http://stackoverflow.com/questions/40470895/phpstorm-saving-with-linux-line-ending-on-windows

0
Comment actions Permalink

If you are using Git, also check your core.eol and core.autocrlf configurations.

Because I run my code in Vagrant Linux environemtn, and not Windows itself, I have these set like so:

git config core.eol lf
git config core.autocrlf input
4
Comment actions Permalink

I confirm the settings - then I get this when switching (again) to LF from CRLF in PHPStorm:

"0 files committed. Warning: LF will be replaced by CRLF in path/to/file.php. The file will have its original line endings in your working directory."

 

0
Comment actions Permalink

Let's try to reproduce this on an empty project. Please create one, set the line separator to \n, restart PhpStorm and create a couple of files. Does it still switch unexpectedly?

0
Comment actions Permalink

I read a stack overflow post on this same topic that led me to believe that I have everything configured correctly, and the software is actually working fine, it's just the error message (and possibly the UI) are misleading or confusing.

The SO post said that line endings (LF) will remain correct in the remote repository but switch to windows (CRLF) when checking out and get switched back upon push.

http://stackoverflow.com/questions/1967370/git-replacing-lf-with-crlf

0
Comment actions Permalink

> every time I reboot and start working on a new file, I see CRLF in the little selector

Are you talking about existing project with files in LF or you mean a new file?

What do you have in File | Settings | Editor | Code Style > Line separator 

2
Comment actions Permalink

This is the solution that worked for me. From here:

 

The proper way to get LF endings in Windows is to first set core.autocrlf to false:

git config --global core.autocrlf false

You need to do this if you are using msysgit, because it sets it to true in its system settings.

Now git won’t do any line ending normalization. If you want files you check in to be normalized, do this: Set text=auto in your .gitattributes for all files:

* text=auto

And set core.eol to lf:

git config --global core.eol lf

Now you can also switch single repos to crlf (in the working directory!) by running

git config core.eol crlf

After you have done the configuration, you might want git to normalize all the files in the repo. To do this, go to to the root of your repo and run these commands:

git rm --cached -rf .
git diff --cached --name-only -z | xargs -n 50 -0 git add -f

If you now want git to also normalize the files in your working directory, run these commands:

git ls-files -z | xargs -0 rm
git checkout .
3
Comment actions Permalink

Thanks, Geoff Hoffman for your complete and well-explained solution. doing the above converted a project I had already pulled from git before realizing I was walking into a CRLF issue because the code is Linux based, and it converted everything to work as expected. Was working in CLion, so this answer is relevant to that platform. My code is now building without issues. Thanks!

0
Comment actions Permalink

A few other things I wanted to add here, since my own question is coming up on the top of search results.

In the lower right-hand corner of the code editor window is a drop down picker for changing the line endings of the current file. You can change a file from CRLF to LF or N here.

Also, another thing I discovered is that you can have a default scheme in PhpStorm that is different from the project settings, so when you go to File | Settings | Editor | Code Style > Line separator be sure to also check to see if the settings are using the default scheme or the project scheme. You may need to switch the scheme to ensure your LF's stay LF and vice versa.

1
Comment actions Permalink

The root problem has nothing to do with Git so I'm still looking for a solution from Jetbrains.  For instance, if you use the remote toolchain feature of CLion to sync a Windows IDE to a Linux workstation it will blindly create any new files with CRLF endings and push them to Linux without version control even knowing.  The only solution seems to be changing the setting for every new file created within the IDE.  I'd prefer an option that lets me make LF endings the default within Jetbrains' IDEs themselves.

0
Comment actions Permalink

@Nthexwn

This could be specific to CLion so better post it in CLion forum.

But in general, please check if this will do the job (same link as in my first comment): http://stackoverflow.com/questions/40470895/phpstorm-saving-with-linux-line-ending-on-windows

0
Comment actions Permalink

That does help actually, thanks!  Part of the problem with all these Jetbrains tools is that they have an ~80% overlap in functionality, so if you search for help with one you find hits for one of the others which may or may not help.  That's how I ended up here.

0

Please sign in to leave a comment.