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

How to keep ^M when to create a new file?

我的文件 target.html.
每行末尾都有 ^M ^M恰好是vim显示0xD的方式,\r\n用于windows中的换行。

head -n 10 target.html > new.html

vim new.htmlset listnew.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                                ">..

感谢 Amadan,我的 target.html 中有些行仅以 LF 结尾。

target.html: HTML document, ISO-8859 text, with CRLF, LF line terminators

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

new.html: HTML document, ASCII text, with CRLF line terminators

当你剪切前 10 行时,碰巧它们都有 CRLF 结尾。 Vim 愉快地结束 "this should be a DOS file!",设置 ff=dos 并且不向您显示 ^M,因为它现在是行终止符的一部分。

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

检查 Vim 的想法

顺便说一下,您可以使用 /^M\@<!$(其中 ^MCtrl-V输入).