Why is vim creating files with DOS line endings?

19,117

Setting only fileformat may not be enough, depending on a few factors. Try this:

set fileformat=unix
set fileformats=unix,dos
"set nobinary

To understand what these do, have a look at :help fileformats, etc.

I think I am able to reproduce your issues, using the vim.exe provided by git in windows. Using the above settings fixed the problem for me. In the example set nobinary is commented out because I don't think you need it, I left it there as a tip that might help in case you need to investigate further.

Share:
19,117

Related videos on Youtube

iconoclast
Author by

iconoclast

Contractor at Infinite Red, a mobile app & web site design & development company with employees worldwide, experts in React Native, Rails, Phoenix, and all things JavaScript!

Updated on September 18, 2022

Comments

  • iconoclast
    iconoclast over 1 year

    I have

    set fileformat=unix
    

    in my .vimrc, and yet if I create a file in vim (running in Cygwin) it persists in creating DOS-format files, and I have to run them through dos2unix to clean them. (I guess I don't really have to, since Git cleans them up for me, but it just bugs me to death that a Unix text editor, which I've specifically instructed to use Unix line endings, is polluting them with extra garbage.)

    Why would this happen? How can I fix it? (And by "fix" I do not mean something that requires me to convert each file individually. That's not a fix. That's what's totally broken.)

    • Matt
      Matt almost 12 years
      I would wager this is a cygwin problem. There is an option in cygwin to choose the line ending style but I hven't seen any evidence it actually works. My university uses Eclipse with cygwin for the GNU compiler collection and I encountered a weird behavior with getline which turned out to be cygwin doing something weird. Resulted in a CR+LF instead of the Unix LF or even a dos style LF+CR. You should check and see what line ending style you have configured in cygwin but YMMV. See also cygwin.com/faq/faq-nochunks.html#faq.api.cr-lf
    • echristopherson
      echristopherson almost 12 years
      @Matt: DOS/Windows does use CR+LF, not LF+CR.
    • Matt
      Matt almost 12 years
      I was afraid I'd swap those if I didn't double check, been over a semester since I had that issue. The line endings were different when running in cygwin and when running at the dos prompt and I remember being convinced it was backwards from what you'd expect from DOS but as you can see they're easy for me to switch up.
    • user
      user almost 12 years
      Are you sure that this .vimrc is even read? Try making a deliberate error and see if vim picks up on it.
    • iconoclast
      iconoclast almost 12 years
      @MichaelKjörling: yeah, I tested, and it is being read. I commented out my set number line, and it took effect, and uncommented it out and that took effect. I also added "blah blah blah" at the bottom, and got "Press ENTER or type command to continue" when I opened anything, but oddly there was no error message before that (unless it was black letters on black background).