[VIM] Removing new line at the End Of File in VIM


Vim is the best text editor when it comes to speed, handy and features comparison with any other text editor in this world. You will understand this if you are a UNIX user, administrator or a developer and more importantly if you really know how powerful VIM is. Anyways, today I am going to tell you guys how to remove the newline added by VIM automatically and how does this really impact???

There are different c coding standards for different projects. For example in Linux
a new continuation line of code is indented with tabs but the same in FreeBSD is done using 4 white spaces.

Similarly in some projects GCC or any other compiler that is used, is configured to through a compiler WARNING if there is no new line at the end of file. This holds true for UNIX like OS for example: Linux FreeBSD etc. But at some places it is configured not to have a new line, where you will get a compiler warning when you compile a file which may have a new line at the end of file.

Sometimes these kind of stuffs are not configured in compiler because at production level projects, compilers are configured to treat compiler warnings as ERRORs. So, these new line stuff are checked via coding standard utilities or scripts.

What’s the big deal if VIM is adding a new line at the end of file?

Earlier I was working on Linux and FreeBSD projects but recently I was moved to a proprietary embedded system project where everything is customized and I got compiler warning after doing some modifications in some of the c and header files using VIM. Earlier, everything was fine. Later I noticed that VIM is adding an extra line at the End of File which was causing problem for me.

How to get rid of automatic new line added or removed by VIM

Run these following commands in VIM:

:set binary
:set noeol
:wq

Please NOTE that the above commands are VIM commands and not shell commands.

Basically, we set the file as a binary file, then we command VIM not to have EOL (End Of Line) and then write and quit the file.

This will change only for the particular file that you open explicitly. If you want to have these changes set for all the file you edit with VIM then you need to add the following lines to your ~/.vimrc file:

set binary
set noeol

Leave a Comment