24

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

iconoclast
  • 9,198
  • 13
  • 57
  • 97
  • 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 http://cygwin.com/faq/faq-nochunks.html#faq.api.cr-lf – Matt Aug 03 '12 at 01:52
  • 1
    @Matt: DOS/Windows does use CR+LF, not LF+CR. – echristopherson Aug 03 '12 at 03:17
  • 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. – Matt Aug 03 '12 at 04:55
  • Are you sure that this .vimrc is even read? Try making a deliberate error and see if vim picks up on it. – user Aug 03 '12 at 12:41
  • @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). – iconoclast Aug 03 '12 at 13:52

1 Answers1

31

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.

janos
  • 11,341