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:

  1. :%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:

  1. :%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


6 Responses to Converting line-endings with ViM

  1. 645 Nic en alex 2008-07-28 6:11 pm

    spannend stuk.. we begrijpen het helemaal

  2. 641 Wez Furlong 2008-07-28 9:47 pm

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

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

    to convert the line endings.

  3. 642 Evert 2008-07-28 9:55 pm

    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

  4. 643 derek fong 2008-07-29 2:53 pm

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

  5. 644 Aaron Rosenfeld 2008-07-29 8:47 pm

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

  6. 646 KeeKee 2008-09-02 2:40 pm

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

    KeeKee



About

My name is Evert, and I've been writing semi-regularly on this blog since 2006.

I'm currently available for contract work.

more info.

Subscribe

Dropbox

Dropbox is a simple cross-platform online backup and sync application. The first 2GB of space is free, and both you and me get an extra 250MB extra space if you sign up through this link.