Unix (LF) vs. Windows (CRLF) line endings-- how to control?

How does PyCharm handle files with line endings different from the host machine's default?  For example, on a Windows machine if I edit a file which originated on unix, what changes (if any) will PyCharm make to line endings of the file? Also, can I customize that behavior like I can in Visual Studio?

I'm asking because it seems like PyCharm has converted one of my source files (which was originally either 100% unix line endings or a mix of unix and windows endings) to use Windows endings. I'd like to prevent this behavior, so PyCharm will always default to unix endings even if there's a mix of endings in a file. (I don't want to convert endings of lines I don't touch, because that will show up as extra diffs when I check back into GIT)

4 comments

Hello Justin,

PyCharm always saves files with the same line endings for every line. It

keeps the line endings that were used in the file when opening, and if a

file has mixed line endings, it uses the line ending from the last line of

the file. At the moment this behavior is not configurable.

How does PyCharm handle files with line endings different from the

host machine's default?  For example, on a Windows machine if I edit a

file which originated on unix, what changes (if any) will PyCharm make

to line endings of the file? Also, can I customize that behavior like

I can in Visual Studio?

I'm asking because it seems like PyCharm has converted one of my

source files (which was originally either 100% unix line endings or a

mix of unix and windows endings) to use Windows endings. I'd like to

prevent this behavior, so PyCharm will always default to unix endings

even if there's a mix of endings in a file. (I don't want to convert

endings of lines I don't touch, because that will show up as extra

diffs when I check back into GIT)

--

Dmitry Jemerov

Development Lead

JetBrains, Inc.

http://www.jetbrains.com/

"Develop with Pleasure!"

0

Hi Dmitry - for mixed line endings, the existing behavior results in unintended changes to files being committed to source control, which can get the developer in trouble when committing patches, when working with automated testing tools, etc. It's the difference between a one-line change and a 500-line change!

A better approach would be to detect the most common line ending in a file, and to use that line ending style, instead of only using the last line's ending.

Even better would be to apply the new line-ending style to only new lines in a file (using the most common line ending for the new lines), and never rewriting line endings of existing lines.

Another good feature would be to warn users when opening and/or saving files with mixed line endings, and let the user choose the desired line ending style (including a default "leave as mixed" which applies the algorithm above).

Finally, it'd be nice to offer a feature like Visual Studio does, where you can "Save As..." a file with a specific line ending style.

0

Hello Justin,

We do not plan to support mixed line endings within a single file. Such state

of a file is broken anyway, and IMO should be fixed sooner than later. And

it would be very expensive for us to support internally.

Using the most common line ending instead of the one from the last line should

be pretty easy to do; YouTrack request is welcome at http://youtrack.jetbrains.net/

Hi Dmitry - for mixed line endings, the existing behavior results in

unintended changes to files being committed to source control, which

can get the developer in trouble when committing patches, when working

with automated testing tools, etc. It's the difference between a

one-line change and a 500-line change!

A better approach would be to detect the most common line ending in a

file, and to use that line ending style, instead of only using the

last line's ending.

Even better would be to apply the new line-ending style to only new

lines in a file (using the most common line ending for the new lines),

and never rewriting line endings of existing lines.

Another good feature would be to warn users when opening and/or saving

files with mixed line endings, and let the user choose the desired

line ending style (including a default "leave as mixed" which applies

the algorithm above).

--

Dmitry Jemerov

Development Lead

JetBrains, Inc.

http://www.jetbrains.com/

"Develop with Pleasure!"

0

Hi Dmitry - I reported two issues in YouTrack: PY-2249  to change the default behavior to most-common-ending, and PY-2250 for longer-term improvements to give users more warning and control over line endings.

0

Please sign in to leave a comment.