Reputation: 685
Which line endings should be used for platform independent code (not file access)?
My next project must run on both Windows and Linux.
I wrote code on Linux and used Hg to clone to Windows and it ran fine with Linux endings. The downside is that if you open the file in something other than a smart editor the line endings are not correct.
Upvotes: 2
Views: 1402
Reputation: 4160
As John Messenger states, newlines (\n
) as opposed to carriage-return/linefeeds (\n\r
) are pretty much the canonical line ending everywhere, except for dedicated Windows-only enclaves.
To support this, git
has the core.autocrlf
option. When enabled on Windows systems, files with standard newline endings will be converted to CRLF when checked out, and converted back on the way back in.
This seems to be a seamless way to cope with the problem of Windows line-endings when running GUI editors and IDEs on Windows. Your editor is probably expecting CRLF line endings on Windows and will get them when using git
with this option, which I believe is set to true
by default on Windows installs.
If you're not using Git, maybe it's time you did? Even when working alone it has lots of value.
However, some users will be using GitBash / Cygwin / Ming tools on Windows, possibly with GitBash supplied vim
, and will not appreciate CRLF line-endings. Those users can turn core.autocrlf
off (false
) before cloning a repo to avoid inappropriate "corrections", and thereby see proper newline-only line endings when editing files. This may also help when using other Linux tools when running on Windows that also expect newline line endings.
Git has three levels of settings: "system" (probably set in C:\Program Files\...
), "global" (set in your home directory Git config file and affecting all your repos) and "local" set in each repo's .git/config
file. Latter levels override the former levels, which can be helpful if your organisation has locked down your C drive.
Here are some of the Git commands to query and update your settings (and you can just edit the configuration files themselves as well):
git config --list --show-origin
: Lists all your settings and the file locations where they are set. A setting may be repeated so the lower level will override the higher.git config --get core.autocrlf
: Get the effective setting from the combination of settings you currently have in place.git config --system core.autocrlf false
: Switch off automatic conversion at the system level, for all repos.git config --global core.autocrlf false
: Switch off automatic conversion in all repos from your home directory config file.Remember, set core.autocrlf
to what you need before you clone. Reference here: https://docs.github.com/en/get-started/getting-started-with-git/configuring-git-to-handle-line-endings
Upvotes: 0
Reputation: 351
In general newlines (as typically used in Linux) are more portable than carriage return and then newline (as used in Windows). Note also that if you store your code on GitHub or another Git repository, it will convert it properly without you having to do anything.
Upvotes: 1