subscribe

Converting line-endings with ViM

I got my hands on a file containing OS/X line-endings (\r), which needed to be converted into Unix line-endings (\n).

Normally I would just do a simple search and replace, with:

:%s/\r/\n/g

Oddly enough, this actually gave me null-characters (0x00) instead of the expected \n. After some browsing, this seems to be the correct command:

:%s/\r/\r/g

I have no clue why, and this seems wrong to me, as this command should not have any effect (replacing \r with \r). But yea, it worked :S

Web mentions

Comments

  • Nic en alex

    spannend stuk.. we begrijpen het helemaal
  • Wez Furlong

    Wez Furlong

    in your vimrc:
    :set fileformats=mac,unix,dos

    then when you get a problematic file:
    :set fileformat=unix

    to convert the line endings.
  • Evert

    Evert

    Thats smart, thanks!

    Still confused about the fact that the latter replace worked though.. Maybe it actually has something to do with that fileformat setting.

    Evert
  • derek fong

    derek fong

    A minor correction: OS X uses Unix-standard \n line-endings. OS 9 used \r line-endings.
  • Aaron Rosenfeld

    Aaron Rosenfeld

    Thank you for this. I have been using :set fileformat=dos for a while but that felt hacky. This worked like a charm. Thanks!
  • KeeKee

    KeeKee

    Now I know what was wrong. Thanks for the help. For some crazy reason, it works and that's all that matters.

    KeeKee