Vim 如何在创建新文件时保留^M?

Vim 如何在创建新文件时保留^M?,vim,newline,Vim,Newline,在我的文件target.html的每一行末尾都有^M ^M恰好是vim显示0xD的方式,\r\n用于windows中的新行 vim new.html和set list用于new.html 为什么没有^M保存在new.html文件中 file target.html target.html: HTML document, ISO-8859 text, with CRLF, LF line terminators file new.html new.html: HTML document, AS

在我的文件
target.html
的每一行末尾都有
^M

^M恰好是vim显示0xD的方式,
\r\n
用于windows中的新行

vim new.html
set list
用于
new.html

为什么没有
^M
保存在
new.html
文件中

file target.html
target.html: HTML document, ISO-8859 text, with CRLF, LF line terminators
file new.html
new.html: HTML document, ASCII text, with CRLF line terminators
让我困惑的是,每行的
new.html
结尾都是
0d0a
,为什么用我的vim打开它时没有显示
^M

xxd new.html
00000000: 3c68 746d 6c20 786d 6c6e 733a 763d 2275  <html xmlns:v="u
00000010: 726e 3a73 6368 656d 6173 2d6d 6963 726f  rn:schemas-micro
00000020: 736f 6674 2d63 6f6d 3a76 6d6c 220d 0a78  soft-com:vml"..x
00000030: 6d6c 6e73 3a6f 3d22 7572 6e3a 7363 6865  mlns:o="urn:sche
00000040: 6d61 732d 6d69 6372 6f73 6f66 742d 636f  mas-microsoft-co
00000050: 6d3a 6f66 6669 6365 3a6f 6666 6963 6522  m:office:office"
00000060: 0d0a 786d 6c6e 733a 773d 2275 726e 3a73  ..xmlns:w="urn:s
00000070: 6368 656d 6173 2d6d 6963 726f 736f 6674  chemas-microsoft
00000080: 2d63 6f6d 3a6f 6666 6963 653a 776f 7264  -com:office:word
00000090: 220d 0a78 6d6c 6e73 3a6d 3d22 6874 7470  "..xmlns:m="http
000000a0: 3a2f 2f73 6368 656d 6173 2e6d 6963 726f  ://schemas.micro
000000b0: 736f 6674 2e63 6f6d 2f6f 6666 6963 652f  soft.com/office/
000000c0: 3230 3034 2f31 322f 6f6d 6d6c 220d 0a78  2004/12/omml"..x
000000d0: 6d6c 6e73 3d22 6874 7470 3a2f 2f77 7777  mlns="http://www
000000e0: 2e77 332e 6f72 672f 5452 2f52 4543 2d68  .w3.org/TR/REC-h
000000f0: 746d 6c34 3022 3e0d 0a0d 0a3c 6865 6164  tml40">....<head
00000100: 3e0d 0a3c 6d65 7461 2068 7474 702d 6571  >..<meta http-eq
00000110: 7569 763d 436f 6e74 656e 742d 5479 7065  uiv=Content-Type
00000120: 2063 6f6e 7465 6e74 3d22 7465 7874 2f68   content="text/h
00000130: 746d 6c3b 2063 6861 7273 6574 3d67 6232  tml; charset=gb2
00000140: 3331 3222 3e0d 0a3c 6d65 7461 206e 616d  312">..<meta nam
00000150: 653d 5072 6f67 4964 2063 6f6e 7465 6e74  e=ProgId content
00000160: 3d57 6f72 642e 446f 6375 6d65 6e74 3e0d  =Word.Document>.
00000170: 0a3c 6d65 7461 206e 616d 653d 4765 6e65  .<meta name=Gene
00000180: 7261 746f 7220 636f 6e74 656e 743d 224d  rator content="M
00000190: 6963 726f 736f 6674 2057 6f72 6420 3132  icrosoft Word 12
000001a0: 223e 0d0a                                ">..
xxd new.html
00000000:3c68 746d 6c20 786d 6c6e 733a 763d 2275。。。。。。。。。
00000170:0a3c 6d65 7461 206e 616d 653d 4765 6e65。。。
感谢Amadan,在我的
target.html

这就是线索。似乎您的
target.html
混合了CRLF结尾和LF结尾。这会混淆Vim,并使其确定
fileformat=unix
,并将
^M
显示为恰好位于行尾的字符

当您剪切前10行时,碰巧它们都有CRLF结尾。Vim愉快地总结道:“这应该是一个DOS文件!”,设置
ff=DOS
,并且不会向您显示
^M
,因为它现在是行终止符的一部分

就像您可以检查
文件的想法一样,您也可以使用
:set ff?

顺便说一句,您可以使用
/^M@(其中
^M
是Ctrl-VEnter)查找有问题的行(使用LF而不是CRLF的行)

这就是线索。似乎您的
target.html
混合了CRLF结尾和LF结尾。这会混淆Vim,并使其确定
fileformat=unix
,并将
^M
显示为恰好位于行尾的字符

当您剪切前10行时,碰巧它们都有CRLF结尾。Vim愉快地总结道:“这应该是一个DOS文件!”,设置
ff=DOS
,并且不会向您显示
^M
,因为它现在是行终止符的一部分

就像您可以检查
文件的想法一样,您也可以使用
:set ff?


顺便说一句,您可以使用
/^M@(其中
^M
是Ctrl-VEnter)查找有问题的行(使用LF而不是CRLF的行).

您能为
target.html
new.html
提供
文件
命令的输出吗?您能为
target.html
new.html
提供
文件
命令的输出吗?我发现有些行仅以
LF
结尾。对于new.html,要打开它并设置ff=unix,
^M
将再次显示。我发现某些行仅以
LF
结尾。对于new.html,要打开它并设置ff=unix,
^M
将再次显示。
xxd new.html
00000000: 3c68 746d 6c20 786d 6c6e 733a 763d 2275  <html xmlns:v="u
00000010: 726e 3a73 6368 656d 6173 2d6d 6963 726f  rn:schemas-micro
00000020: 736f 6674 2d63 6f6d 3a76 6d6c 220d 0a78  soft-com:vml"..x
00000030: 6d6c 6e73 3a6f 3d22 7572 6e3a 7363 6865  mlns:o="urn:sche
00000040: 6d61 732d 6d69 6372 6f73 6f66 742d 636f  mas-microsoft-co
00000050: 6d3a 6f66 6669 6365 3a6f 6666 6963 6522  m:office:office"
00000060: 0d0a 786d 6c6e 733a 773d 2275 726e 3a73  ..xmlns:w="urn:s
00000070: 6368 656d 6173 2d6d 6963 726f 736f 6674  chemas-microsoft
00000080: 2d63 6f6d 3a6f 6666 6963 653a 776f 7264  -com:office:word
00000090: 220d 0a78 6d6c 6e73 3a6d 3d22 6874 7470  "..xmlns:m="http
000000a0: 3a2f 2f73 6368 656d 6173 2e6d 6963 726f  ://schemas.micro
000000b0: 736f 6674 2e63 6f6d 2f6f 6666 6963 652f  soft.com/office/
000000c0: 3230 3034 2f31 322f 6f6d 6d6c 220d 0a78  2004/12/omml"..x
000000d0: 6d6c 6e73 3d22 6874 7470 3a2f 2f77 7777  mlns="http://www
000000e0: 2e77 332e 6f72 672f 5452 2f52 4543 2d68  .w3.org/TR/REC-h
000000f0: 746d 6c34 3022 3e0d 0a0d 0a3c 6865 6164  tml40">....<head
00000100: 3e0d 0a3c 6d65 7461 2068 7474 702d 6571  >..<meta http-eq
00000110: 7569 763d 436f 6e74 656e 742d 5479 7065  uiv=Content-Type
00000120: 2063 6f6e 7465 6e74 3d22 7465 7874 2f68   content="text/h
00000130: 746d 6c3b 2063 6861 7273 6574 3d67 6232  tml; charset=gb2
00000140: 3331 3222 3e0d 0a3c 6d65 7461 206e 616d  312">..<meta nam
00000150: 653d 5072 6f67 4964 2063 6f6e 7465 6e74  e=ProgId content
00000160: 3d57 6f72 642e 446f 6375 6d65 6e74 3e0d  =Word.Document>.
00000170: 0a3c 6d65 7461 206e 616d 653d 4765 6e65  .<meta name=Gene
00000180: 7261 746f 7220 636f 6e74 656e 743d 224d  rator content="M
00000190: 6963 726f 736f 6674 2057 6f72 6420 3132  icrosoft Word 12
000001a0: 223e 0d0a                                ">..
target.html: HTML document, ISO-8859 text, with CRLF, LF line terminators
new.html: HTML document, ASCII text, with CRLF line terminators