简体   繁体   English

在 Mac 上,行尾自动设置为 Windows 行尾,我该如何更改?

[英]On Mac, line endings are automatically being set to Windows line endings, how do I change that?

Every time I git commit anything I get a warning that my files have windows line endings and they will be changed to non-Windows line endings.每次我git commit任何内容时,我都会收到一条警告,指出我的文件有 windows 行尾,它们将更改为非 Windows 行尾。 I don't know why this is even happening in the first place, it seems every file I write in now has windows line endings.我不知道为什么会发生这种情况,似乎我现在写入的每个文件都有 windows 行结尾。 How do I change this back to use Unix line endings?如何将其更改回使用 Unix 行尾? This also happens regardless of editor, it happens in both Vim and VS Code无论编辑器如何,都会发生这种情况,它发生在 Vim 和 VS Code 中

Both VIM and VS Code recognize whether the file was using macOS/Unix line endings (LF, eg 0a only) or Windows file endings (CRLF, eg 0d followed by 0a ) and preserves that. VIM 和 VS 代码都识别文件是使用 macOS/Unix 行结尾(LF,例如仅0a )还是 Windows 文件结尾(CRLF,例如0d后跟0a )并保留它。 You can use terminal hexdump command and see if your lines are separated by 0d 0a or only 0a .您可以使用终端hexdump命令并查看您的行是否由0d 0a或仅0a分隔。

Most likely, when the files were first created, they were saved with Windows newline sequence, and VIM and VS Code are merely preserving that setting.最有可能的是,当文件第一次创建时,它们是用 Windows 换行序列保存的,而 VIM 和 VS Code 只是保留了该设置。

  • In VIM, when you open/save the file, if the file is currently using the Windows CRLF , you'll see a little [dos] in the status message at the bottom.在 VIM 中,当您打开/保存文件时,如果文件当前正在使用 Windows CRLF ,您会在底部的状态消息中看到一点[dos] If it already is in unix format, you won't see the [dos] there.如果它已经是 unix 格式,您将不会在那里看到[dos]

    在此处输入图像描述

    In VIM, if it is a [dos] file, you can use the :set fileformat=unix to convert it to a Unix end-of-line character, and then save the converted file.在 VIM 中,如果是[dos]文件,可以使用:set fileformat=unix将其转换为 Unix 行尾字符,然后保存转换后的文件。 See Vim convert file from DOS to UNIX .请参阅Vim 将文件从 DOS 转换为 UNIX

  • In VS Code, you can set the eol setting, but that only affects new files.在 VS Code 中,您可以设置eol设置,但这只会影响新文件。 Again, existing files will preserve whatever end-of-line setting the file originally was using.同样,现有文件将保留文件最初使用的任何行尾设置。

    But, if you open a file and look on the status bar, you will see a LF or CRLF towards the right.但是,如果您打开一个文件并查看状态栏,您会在右侧看到一个LFCRLF You can click on that to toggle back and forth.您可以单击它来来回切换。

    在此处输入图像描述

  • For information about how this is handled on the git side, see Configuring Git to handle line endings .有关如何在 git 端处理此问题的信息,请参阅配置 Git 以处理行尾 I suspect that you might want to stick with the existing behavior (updating to Unix end-of-line character), but you will see your options clearly laid out in that GitHub doc.我怀疑您可能想要坚持现有的行为(更新到 Unix 行尾字符),但您会在 GitHub 文档中清楚地看到您的选项。 But if you really want to keep the CRLF in your files, you can configure your repo accordingly.但是如果你真的想在你的文件中保留 CRLF,你可以相应地配置你的 repo。

Depending upon how many files you have, you might want to find a tool to convert from Windows/DOS end-of-line CRLF sequences to Unix/macOS LF characters.根据您拥有的文件数量,您可能希望找到一种工具来将 Windows/DOS 行尾 CRLF 序列转换为 Unix/macOS LF 字符。 A quick StackOverflow or web search will undoubtedly give you lots of suggestions.快速的 StackOverflow 或 web 搜索无疑会给你很多建议。

Or, it sounds like your repo has already been updated to Unix-style LF characters, so you might just want to pull and use that.或者,听起来您的 repo 已经更新为 Unix 风格的 LF 字符,所以您可能只想拉取并使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM