linux 查看换行符是LF,window/linux 回车换行符 CRLF和LF

目录:

什么是CRLF和LF

为什么要探究CRLF和LF

三种方式处理的不同

更多

参考文献

1、什么是CRLF和LF

LF是line feed的缩写,中文意思是换行。

2、为什么要探究CRLF和LF

在学习git软件,安装git到configuring the lien ending conversion时,有三个选项。

a. Checkout Windows-style,commit Unix-style line endings.

b.Checkout as-is,commit Unix-style line endings.

c.Checkout as-is,commit as-is line endings.

这里面讲到了做两个操作(Checkout,Commit)的三种处理line endings的操作(Windows-style,Unix-style,As-is)。

为什么会出现这三种处理line endings(行尾结束符)呢?在Git的帮助页面给出了很好的解释。

Reference From:https://help.github.com/articles/dealing-with-line-endings

If you're using Git to collaborate with others on GitHub, ensure that Git isproperly configured to handle line endings.

Every time you press return on your keyboard you're actuallyinserting an invisible character called a line ending. Historically, differentoperating systems have handled line endings differently.

When you view changes in a file, Git handles line endings in its own way.Since you're collaborating on projects with Git and GitHub, Git mightproduce unexpected results if, for example, you're working on a Windows machine,and your collaborator has made a change in OS X.

意思很好理解,就不翻译了。重视由于历史的原因,各种不同的操作系统在处理行尾结束符采取了不同的处理方法。而Git和GitHub

3、三种方式处理的不同

CRLF->Windows-style

LF->Unix Style

CR->Mac Style

CRLF表示句尾使用回车换行两个字符(即我们常在Windows编程时使用"\r\n"换行)

LF表示表示句尾,只使用换行.

CR表示只使用回车.

4、在Git中如何转换?

在Git通过下面的命令配置

$git config --global core.autocrlf true

# Configure Git on Windows to properly handle line endings

解释:core.autocrlf是git中负责处理line endings的变量,可以设置三个值--true,inout,false.

设置成三个值会有什么效果呢?

If core.autocrlf is set to true, that means that any time you add a file to the git repo that git thinks is a text file, it will turn all CRLF line endings to just LF before it stores it in the commit.。

设置为true,添加文件到git仓库时,git将其视为文本文件。他将把crlf变成lf。【2】

If core.autocrlf is set to false, no line-ending conversion is ever performed, so text files are checked in as-is. This usually works ok。【2】

设置为false时,line-endings将不做转换操作。文本文件保持原来的样子。

设置为input时,添加文件git仓库石,git把crlf编程lf。当有人Check代码时还是lf方式。因此在window操作系统下,不要使用这个设置。

这是参考文献2给的解释希望能帮助大家。

Yet another way to show how autocrlf works

1) true: x -> LF -> CRLF

2) input: x -> LF -> LF

3) false: x -> x -> x

where x is either CRLF (windows-style) or LF (unix-style) and arrows stand for

file to commit -> repository -> checked out file

更多:

更为复杂的配置命令见网站:https://www.kernel.org/pub/software/scm/git/docs/git-config.html

关于LF和CRLF讨论见:http://stackoverflow.com/questions/1967370/git-replacing-lf-with-crlf

You can also provide a special --global flag, which makes Git usethe same settings for line endings across every local Git repository on your computer.

参考文献

【1】https://help.github.com/articles/dealing-with-line-endings

【2】http://stackoverflow.com/questions/1967370/git-replacing-lf-with-crlf

查看一个文件的换行符LF还是CRLF可以通过多种方式,具体方法取决于你使用的操作系统和可用的工具。以下是一些常见的方法: 1. 在Windows系统中,你可以使用命令行工具如`type`命令,然后通过`findstr`命令的`/n`参数来查看文件的行号,行号之间的分隔符可以显示是LF还是CRLF。例如,你可以打开命令提示符并输入以下命令: ``` type 文件名.txt | findstr /n /r /c:"^" 文件名.txt ``` 这将输出文件内容,并在每行的开始显示行号。如果行号之间是LF,则表示文件使用LF换行符;如果是CR LF,则表示使用CRLF换行符。 2. 在Linux或macOS系统中,你可以使用`cat`命令的`-vE`参数,它会显示文本文件中的非打印字符,包括换行符。例如: ``` cat -vE 文件名.txt ``` 如果你看到行尾显示`$`符号,表示该行结束于LF;如果显示`^M$`(其中`^M`是由Ctrl+V和Ctrl+M输入的),则表示该行结束于CRLF。 3. 使用文本编辑器或IDE(如Visual Studio Code、Sublime Text、IntelliJ IDEA等)打开文件,这些工具通常会提供查看和编辑文件换行符的功能,并且可以直观地显示文件内容。 4. 使用十六进制编辑器或查看器(如Notepad++、HxD等),可以直接查看文件的字节数据,LF换行符在十六进制中表示为0x0A,而CRLF则是0x0D后跟0x0A。 5. 如果你熟悉命令行工具,可以使用`od`(八进制转储)命令在Linux或macOS系统中查看文件的字节表示: ``` od -t x1 文件名.txt ``` 这个命令会以十六进制形式显示文件内容,你可以查找连续的0A(表示LF)或0D 0A(表示CRLF)来判断换行符类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值