Windows和Linux的换行符CRLF/LF(\r\n,\n)简介

问题:

有的时候, 我们把一个在windows上修改过的文件拿到linux上用vim打开之后,每行末尾会出现多余的字符 "^M",这是怎么回事呢?

1.CR/LF介绍

CR是Carriage-Return的缩写,即回车;

LF是Line-Feed的缩写,即换行。

CR和LF是在计算机终端还是电传打印机的时候遗留下来的东西。电传打字机就像普通打字机一样工作。

在每一行的末端,CR命令让打印头回到左边。LF命令让纸前进一行。

虽然使用卷纸的终端时代已经过去了,但是,CR和LF命令依然存在,许多应用程序和网络协议仍使用这些命令作为分隔符。

Linux(unix) 和 mac 默认使用 "\n" 作为换行符;

Windows 默认使用 "\r\n" 作为换行符;

2.Unix(Linux)的换行符

Linux 下换行符是 "\n"。

"\n" 在 ACSII表中 对应 LF , ACSII值为 10 ,即0x0a (16进制) 

 

3.windows下换行符

windows 下换行符是 "\r\n"。

"\r" 在ACSII表中对应 "CR", ACSII值为 13 ,即0x0d (16进制)  。

"\r" 在vim中被解释为 "^M" 。

4. unix/windows格式换行符转换

4.1 在linux上可以使用以下工具进行转换

  1. dos2unix : 将windows风格换行符转换为unix风格换行符
  2. unix2dos:  将unix风格换行符转换为windows风格换行符

4.2 在windows上CRLF和LF的转换

4.2.1 使用dos2unix/unix2dos 转换

下载windows版本的 dos2unix/unix2dos,

dos2unix - Browse /dos2unix/7.5.1 at SourceForge.net

使用方法参考dos2unix工具中

dos2unix-7.5.1-win64-nls/share/doc/dos2unix-7.5.1/dos2unix.htm

example 和 RECURSIVE CONVERSION 章节

(见附录2)

4.2.2 在windows上常用的代码编辑器一般都支持CRLF和LF的转换

比如说 VsCode,  在右下角可以选择 LF 或者CRLF;

其他编辑器的操作大同小异。

需要默认设置的话, 在设置里修改

5. git中关于换行符的一些配置

5.1 core.autocrlf

core.autocrlf 选项有三个可选值:

  • true : 提交时改成LF,检出时改成CRLF
  • false (默认值): 提交时是什么就是什么,不改换行符,检出时也不改
  • input:  提交时改成LF,检出时不改

5.2 core.eol

core.eol 选项用于指定文件的行尾样式

  • lf :使用 LF 作为行尾样式。
  • crlf:使用 CRLF 作为行尾样式。
  • native (默认值):使用操作系统的默认行尾样式。

5.3 core.safecrlf

core.safecrlf 选项用于防止混合换行符的错误。它有三个可选值:

  • false: 关闭检查,允许混合换行符的错误。
  • warn (默认值):开启检查,并在发现混合换行符的错误时打印警告信息。
  • true:开启检查,并在发现混合换行符的错误时打印错误信息并拒绝提交。

5.4 git配置建议

一些查看git配置的命令

# 查看 git config 配置
git config -l

# 查看 git config 配置具体位置
git config --list --show-origin

# 全局配置
git config --global core.autocrlf true

5.4.1

开发环境:windows

代码编译/运行环境: windows

建议配置 : core.autocrlf = true

5.4.2

开发环境:windows

代码编译/运行环境: Linux / Mac

建议配置 : core.autocrlf = input

5.4.3

开发环境:Linux / Mac

代码编译/运行环境: Linux / Mac

建议配置 : core.autocrlf = false (保持默认配置)

5.4.4

开发环境:Linux / Mac

代码编译/运行环境: Windows

建议配置 : core.autocrlf = true

个人配置是保持默认配置,

个人工作情况是:

99%概率在linux提交,运行在linux的代码;

有极小概率可能在linux上提交bat脚本;

 因此保持默认配置。

针对在linux环境提交的bat脚本,手动转换为 CRLF格式。

附录1. ASCII 码表

附录2 . dos2unix 使用方法介绍

EXAMPLES
    Read input from 'stdin' and write output to 'stdout':

        dos2unix < a.txt
        cat a.txt | dos2unix

    Convert and replace a.txt. Convert and replace b.txt:

        dos2unix a.txt b.txt
        dos2unix -o a.txt b.txt

    Convert and replace a.txt in ascii conversion mode:

        dos2unix a.txt

    Convert and replace a.txt in ascii conversion mode, convert and replace
    b.txt in 7bit conversion mode:

        dos2unix a.txt -c 7bit b.txt
        dos2unix -c ascii a.txt -c 7bit b.txt
        dos2unix -ascii a.txt -7 b.txt

    Convert a.txt from Mac to Unix format:

        dos2unix -c mac a.txt
        mac2unix a.txt

    Convert a.txt from Unix to Mac format:

        unix2dos -c mac a.txt
        unix2mac a.txt

    Convert and replace a.txt while keeping original date stamp:

        dos2unix -k a.txt
        dos2unix -k -o a.txt

    Convert a.txt and write to e.txt:

        dos2unix -n a.txt e.txt

    Convert a.txt and write to e.txt, keep date stamp of e.txt same as
    a.txt:

        dos2unix -k -n a.txt e.txt

    Convert and replace a.txt, convert b.txt and write to e.txt:

        dos2unix a.txt -n b.txt e.txt
        dos2unix -o a.txt -n b.txt e.txt

    Convert c.txt and write to e.txt, convert and replace a.txt, convert and
    replace b.txt, convert d.txt and write to f.txt:

        dos2unix -n c.txt e.txt -o a.txt b.txt -n d.txt f.txt

RECURSIVE CONVERSION
    In a Unix shell the find(1) and xargs(1) commands can be used to run
    dos2unix recursively over all text files in a directory tree. For
    instance to convert all .txt files in the directory tree under the
    current directory type:

        find . -name '*.txt' -print0 |xargs -0 dos2unix

    The find(1) option "-print0" and corresponding xargs(1) option -0 are
    needed when there are files with spaces or quotes in the name. Otherwise
    these options can be omitted. Another option is to use find(1) with the
    "-exec" option:

        find . -name '*.txt' -exec dos2unix {} \;

    In a Windows Command Prompt the following command can be used:

        for /R %G in (*.txt) do dos2unix "%G"

    PowerShell users can use the following command in Windows PowerShell:

        get-childitem -path . -filter '*.txt' -recurse | foreach-object {dos2unix $_.Fullname}

参考资料:

CRLF_百度百科

百度百科-CRLF

【git系列4/4】如何设置core.autocrlf | core.safecrlf (配置值的含义及最佳实践)

【git系列4/4】如何设置core.autocrlf | core.safecrlf (配置值的含义及最佳实践)-CSDN博客

Git 自动换行符 (autocrlf) 输入是将换行符从 LF 转换为 CRLF 吗

Git 自动换行符 (autocrlf) 输入是将换行符从 LF 转换为 CRLF 吗|极客笔记

Shell脚本中^M的问题和解决方案

Shell脚本中^M的问题和解决方案-CSDN博客

Sourceforge-dos2unix

https://sourceforge.net/projects/dos2unix

  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值