views:

2892

answers:

5

So I work in a PHP shop, and we all use different editors, and we all have to work on windows. I use vim, and everyone in the shop keeps complaining that whenever I edit a file there is a newline at the bottom. I've searched around and found that this is a documented behavior of vi & vim... but I was wondering if there was some way to disable this feature. (It wouldbe best if I could disable it for specific file extensions).

If anyone knows about this, that would be great!

+2  A: 

I have not tried this option, but the following information is given in the vim help system (i.e. help eol):

'endofline' 'eol'   boolean (default on)
      local to buffer
      {not in Vi}

When writing a file and this option is off and the 'binary' option
is on, no <EOL> will be written for the last line in the file.  This
option is automatically set when starting to edit a new file, unless
the file does not have an <EOL> for the last line in the file, in
which case it is reset.

Normally you don't have to set or reset this option. When 'binary' is off the value is not used when writing the file. When 'binary' is on it is used to remember the presence of a for the last line in the file, so that when you write the file the situation from the original file can be kept. But you can change it if you want to.

You may be interested in the answer to a previous question as well: "Why should files end with a newline".

Tim Henigan
I also had come across the eol setting... but it does not accomplish this task. It is more of a variable that determines whether one has already been placed at the end of the file or not. Also, it has no effect on text files, but only on binary files.
Boushley
The vim help also says "When 'binary' is off the value is not used when writing the file." about eol
Boushley
A: 

Maybe you could look at why they are complaining. If a php file has a newline after the ending ?>, php will output it as part of the page. This is not a problem unless you try to send headers after the file is included.

However, the ?> at the end of a php file is optional. No ending ?>, no problem with a newline at the end of the file.

Ken
You are correct, but we as a shop decided that it looks better to have the ending ?> I know we could stop using it... but I'ld prefer to adjust my editor to fit my coding style than the other way around.
Boushley
Also, so you know php will automatically parse your ending tag as ?> or as ?>\n (because in linux all valid files should end with a \n)... So my code doesn't cause these problems... they just don't like the look.
Boushley
+1  A: 

Would it be possible for you to use a special command for saving these files?

If you do :set binary, :w and :set nobinary the file will be written without newline if there was none to start with.

This sequence of commands could be put into a user defined command or a mapping, of course.

Blixtor
Thats an interesting suggestion, I'll let you know.
Boushley
Unfortunately because I'm working with windows, if I save it in binary mode, it forces the save to be in unix mode. Even when I do :set binary :set fileformat=dos :w :set nobinary Once I quit, it saved the file in unix format... I can't believe that there isn't some way to just turn this off.
Boushley
+7  A: 

Add the following command to your .vimrc to turn of the end-of-line option:

autocmd FileType php setlocal noeol binary fileformat=dos

However, PHP itself will ignore that last end-of-line - it shouldn't be an issue. I am almost certain that in your case there is something else which is adding the last newline character, or possibly there is a mixup with windows/unix line ending types (\n or \r\n, etc).

Update:

An alternative solution might be to just add this line to your .vimrc:

set fileformats+=dos
too much php
I know that php isn't parsing it wrong... I've shown that to my coworkers, but winmerge sees them as being different... I'll try that and let you know how it turns out.
Boushley
Doing this has the desired affect of no final line, but it also converts the file to unix line endings... (even on a windows box)...So I had done that before by switching into binary mode... but then the line endings don't show up write in notepad... everything is just one line.
Boushley
Try adding fileformat=dos as well
too much php
Unfortunately neither of those suggestions work. Adding the fileformat=dos to the autocmd has no effect, and set filetype+=dos still adds the trailing \n
Boushley
Sorry, I got that wrong, use 'set filetypes+=dos', not 'set fileformats...'
too much php
+3  A: 

OK, you being on Windows complicates things ;)

As the 'binary' option resets the 'fileformat' option (and writing with 'binary' set always writes with unix line endings), let's take out the big hammer and do it externally!

How about defining an autocommand (:help autocommand) for the BufWritePost event? This autocommand is executed after every time you write a whole buffer. In this autocommand call a small external tool (php, perl or whatever script) that strips off the last newline of the just written file.

So this would look something like this and would go into your .vimrc file:

autocmd!   "Remove all autocmds (for current group), see below"
autocmd BufWritePost *.php !your-script <afile>

Be sure to read the whole vim documentation about autocommands if this is your first time dealing with autocommands. There are some caveats, e.g. it's recommended to remove all autocmds in your .vimrc in case your .vimrc might get sourced multiple times.

Blixtor
Well... I was hoping that there was a better solution than this... but this definitely works!
Boushley
This works great! Yay! However I have one question... is there a way to make it so I don't have to hit enter twice after each save. I have to hit enter in the cmd window that popped up and then again because vim is telling me the script returned successful... Any way to just make them all go away?
Boushley