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?
Please sign in to leave a comment.
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
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:
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."
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?
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
> 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
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
tofalse
: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:And set
core.eol
tolf
:Now you can also switch single repos to crlf (in the working directory!) by running
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:
If you now want git to also normalize the files in your working directory, run these commands:
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!
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.
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.
@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
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.
I originally had the setting Editor → CodeStyle → Line Separator → Unix (\n) enabled on Windows, but it appears the update to IntelliJ 2024.1.1 reverted that setting and created a lot of whitespace diffs in my Java files.
Jan Bauer
Have not noticed that on in my PhpStorm (Windows 10) – still have the same in 2024.1.
1. Could be some issue when syncing your settings in case you are using Settings sync via JetBrains acc or VCS (I'm not using any of these so cannot comment on this moment)
2. Could also be your .editorconfig file in this project (very unlikely IMO). Plus, it would silently override what GUI has (will not change the values displayed there)
Not sure what else might have reset it.