問題:
有的時(shí)候, 我們把一個(gè)在windows上修改過的文件拿到linux上用vim打開之后,每行末尾會(huì)出現(xiàn)多余的字符 "^M",這是怎么回事呢?
1.CR/LF介紹
CR是Carriage-Return的縮寫,即回車;
LF是Line-Feed的縮寫,即換行。
CR和LF是在計(jì)算機(jī)終端還是電傳打印機(jī)的時(shí)候遺留下來的東西。電傳打字機(jī)就像普通打字機(jī)一樣工作。
在每一行的末端,CR命令讓打印頭回到左邊。LF命令讓紙前進(jìn)一行。
雖然使用卷紙的終端時(shí)代已經(jīng)過去了,但是,CR和LF命令依然存在,許多應(yīng)用程序和網(wǎng)絡(luò)協(xié)議仍使用這些命令作為分隔符。
Linux(unix) 和 mac 默認(rèn)使用 "\n" 作為換行符;
Windows 默認(rèn)使用 "\r\n" 作為換行符;
2.Unix(Linux)的換行符
Linux 下?lián)Q行符是 "\n"。
"\n" 在 ACSII表中 對(duì)應(yīng) LF , ACSII值為 10 ,即0x0a (16進(jìn)制)?
?
3.windows下?lián)Q行符
windows 下?lián)Q行符是 "\r\n"。
"\r" 在ACSII表中對(duì)應(yīng) "CR", ACSII值為 13 ,即0x0d (16進(jìn)制)? 。
"\r" 在vim中被解釋為 "^M" 。
4. unix/windows格式換行符轉(zhuǎn)換
4.1 在linux上可以使用以下工具進(jìn)行轉(zhuǎn)換
- dos2unix : 將windows風(fēng)格換行符轉(zhuǎn)換為unix風(fēng)格換行符
- unix2dos:? 將unix風(fēng)格換行符轉(zhuǎn)換為windows風(fēng)格換行符
4.2 在windows上CRLF和LF的轉(zhuǎn)換
4.2.1 使用dos2unix/unix2dos 轉(zhuǎn)換
下載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 章節(jié)
(見附錄2)
4.2.2 在windows上常用的代碼編輯器一般都支持CRLF和LF的轉(zhuǎn)換
比如說 VsCode,? 在右下角可以選擇 LF 或者CRLF;
其他編輯器的操作大同小異。
需要默認(rèn)設(shè)置的話, 在設(shè)置里修改
5. git中關(guān)于換行符的一些配置
5.1 core.autocrlf
core.autocrlf?選項(xiàng)有三個(gè)可選值:
- true : 提交時(shí)改成LF,檢出時(shí)改成CRLF
- false (默認(rèn)值): 提交時(shí)是什么就是什么,不改換行符,檢出時(shí)也不改
- input:? 提交時(shí)改成LF,檢出時(shí)不改
5.2 core.eol
core.eol?選項(xiàng)用于指定文件的行尾樣式
- lf :使用 LF 作為行尾樣式。
- crlf:使用 CRLF 作為行尾樣式。
- native (默認(rèn)值):使用操作系統(tǒng)的默認(rèn)行尾樣式。
5.3 core.safecrlf
core.safecrlf?選項(xiàng)用于防止混合換行符的錯(cuò)誤。它有三個(gè)可選值:
- false: 關(guān)閉檢查,允許混合換行符的錯(cuò)誤。
- warn (默認(rèn)值):開啟檢查,并在發(fā)現(xiàn)混合換行符的錯(cuò)誤時(shí)打印警告信息。
- true:開啟檢查,并在發(fā)現(xiàn)混合換行符的錯(cuò)誤時(shí)打印錯(cuò)誤信息并拒絕提交。
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
開發(fā)環(huán)境:windows
代碼編譯/運(yùn)行環(huán)境: windows
建議配置 : core.autocrlf = true
5.4.2
開發(fā)環(huán)境:windows
代碼編譯/運(yùn)行環(huán)境: Linux / Mac
建議配置 : core.autocrlf = input
5.4.3
開發(fā)環(huán)境:Linux / Mac
代碼編譯/運(yùn)行環(huán)境: Linux / Mac
建議配置 : core.autocrlf = false (保持默認(rèn)配置)
5.4.4
開發(fā)環(huán)境:Linux / Mac
代碼編譯/運(yùn)行環(huán)境: Windows
建議配置 : core.autocrlf = true
個(gè)人配置是保持默認(rèn)配置,
個(gè)人工作情況是:
99%概率在linux提交,運(yùn)行在linux的代碼;
有極小概率可能在linux上提交bat腳本;
?因此保持默認(rèn)配置。
針對(duì)在linux環(huán)境提交的bat腳本,手動(dòng)轉(zhuǎn)換為 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】如何設(shè)置core.autocrlf | core.safecrlf (配置值的含義及最佳實(shí)踐)
【git系列4/4】如何設(shè)置core.autocrlf | core.safecrlf (配置值的含義及最佳實(shí)踐)-CSDN博客
Git 自動(dòng)換行符 (autocrlf) 輸入是將換行符從 LF 轉(zhuǎn)換為 CRLF 嗎
Git 自動(dòng)換行符 (autocrlf) 輸入是將換行符從 LF 轉(zhuǎn)換為 CRLF 嗎|極客筆記
Shell腳本中^M的問題和解決方案
Shell腳本中^M的問題和解決方案-CSDN博客
Sourceforge-dos2unix文章來源:http://www.zghlxwxcb.cn/news/detail-775211.html
https://sourceforge.net/projects/dos2unix文章來源地址http://www.zghlxwxcb.cn/news/detail-775211.html
到了這里,關(guān)于Windows和Linux的換行符CRLF/LF(\r\n,\n)簡介的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!