showkey
showkey

Reputation: 318

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

There are ^M at the end of every line in my file target.html.
^M happens to be the way vim displays 0xD,\r\n used for a new line in windows.

enter image description here

head -n 10 target.html > new.html

vim new.html and set list for the new.html.

enter image description here

why no ^M kept in new.html file?

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

It confused me that the ending in new.html is 0d0a for every line,why no ^M displayed when to open it with my vim?

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                                ">..

Thank to Amadan,some line ended with only LF in my target.html.
enter image description here

Upvotes: 0

Views: 64

Answers (1)

Amadan
Amadan

Reputation: 198496

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

This is the clue. It seems your target.html has a mix of CRLF endings and LF endings. This confuses Vim, and makes it determine that fileformat=unix and displays ^M as a character that happens to be at the end of the line.

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

When you cut the top 10 lines, it so happens that all of them have CRLF ending. Vim happily concludes "this should be a DOS file!", sets ff=dos and doesn't display the ^M to you, as it's now part of the line terminator.

Just like you can check what file thinks, you can check what Vim thinks about it using :set ff?

By the way, you can find the offending line (the one that uses LF instead of CRLF) using /^M\@<!$ (where ^M is Ctrl-VEnter).

Upvotes: 3

Related Questions