Vim Tips Wiki
Advertisement

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created December 31, 2010 · complexity basic · author Jo Liss · version 7.0

By default, Vim adds a newline after the last line of each file. That's great for new files, but when you edit other people's files and they don't have newlines at the end, the diff gets cluttered with an extra chunk like this, because Vim "helpfully" added a newline:

-  }
\ No newline at end of file
+  }

To stop this from happening, add this to your .vimrc:

set binary

Caveats:

  • This will silently convert all line endings to Unix format, so it's probably not usable on Windows.
  • Options textwidth, wrapmargin, modeline and expandtab are reset by :set binary, so make sure that "set binary" comes before setting any of these options.

Always remove newlines at EOF

If you want to always strip newlines from files (even new ones), you can try this:[1]

au BufWritePre * :set binary | set noeol
au BufWritePost * :set nobinary | set eol

References

Comments

Advertisement