Vim no end of line on last line or eof
Asked Answered
S

4

30

I am trying to setup vim to skip adding eol on last line or eof, i have tried this

:set binary
:set noeol
:w

which is not perfect cause binary override filetype for later use.

Any other option to set this, i don't need newline on last line.

Stenophyllous answered 9/11, 2010 at 12:13 Comment(5)
The binary option doesn't do anything to filetype.Lawannalawbreaker
To whom it may concern: Why did you vote to close this? The FAQ tells that “software tools commonly used by programmers” are on-topic.Jandel
@Lawannalawbreaker setting file to binary disable many options, :help binStenophyllous
and none of them are filetype, :help binLawannalawbreaker
Similar: VIM Disable Automatic Newline At End Of File at SOWhose
S
8

This is even better i found somewhere:

au BufWritePre * :set binary | set noeol
au BufWritePost * :set nobinary | set eol
Stenophyllous answered 11/11, 2010 at 9:6 Comment(1)
Also it will be useful for some cases to prepend set noeol in your .vimrc by default.Webbing
S
18

I wanted to add an answer that I think can be as useful. The selected answer always remove the EOL on files even if they had one to begin with. This may be the behavior that you want, it also may not be. In my opinion I want to preserve the EOL as I originally opened the file.

What I suggest is a slight modification. Put set binary at the top of your .vimrc file. This way any files will open in binary mode. If they have no EOL then vim will detect this and leave it NOEOL. If they have an EOL then it'll recognize it as having an EOL and leave it be.

If you would like new files to also not have EOL then you should set,

au BufNewFile * set noeol

The commands for the chap would be if you always want NOEOL ever. The one thing to be aware of is that if you have a file with spaces at the bottom, then you will lose spaces at the end. This is because the following happens,

  1. VIM reads the file and see a newline at the end and thinks, OK, this is a file with a newline (regardless of whether in binary mode or not).
  2. When you then write a file it executes the autocmd,
    1. set binary mode
    2. set noeol (this deletes the EOL that VIM thought was ending the file and deletes it.
    3. the file gets saved
  3. Now you have a file that has one less EOL in it.

This process will repeat until the EOL's are gone and the file ends in a single character. With my setup, what happens is the file is opened and if VIM sees at least one EOL then it keeps it internally. If you save that file, no matter what it is there (you can check by typing set eol?. In the case that you want to get rid of that internally stored EOL you just say set noeol and then save and BOOM, the last EOL is removed.

WHOOO, I am winded typing this.

Sauterne answered 8/1, 2011 at 2:5 Comment(0)
S
8

This is even better i found somewhere:

au BufWritePre * :set binary | set noeol
au BufWritePost * :set nobinary | set eol
Stenophyllous answered 11/11, 2010 at 9:6 Comment(1)
Also it will be useful for some cases to prepend set noeol in your .vimrc by default.Webbing
J
5

From vim documentation:

'binary' 'bin'      boolean (default off)
            local to buffer
            {not in Vi}
    This option should be set **before** editing a binary file.  You can also

You should therefore use vim -b or :e ++bin file, or reload using :e! ++bin.

Jandel answered 9/11, 2010 at 12:17 Comment(4)
Binary doesn't work for me, cause it reset filetype, any other option to disable eol on eof?Stenophyllous
Then use :setf your_file_type to enable it back?Jandel
I need this for php files, and its to much hacking, enabling binary, setting noel, saving file, and than disabling binary and setting file type. I also use NERDTree which i would probably have to hack to get this functionality.Stenophyllous
This is the best answer by far! In 1 sentence you give 3 powerful solutions to the OP's issue. I was able to combine vim -b myfile and :set noeol to meet my needs and now I have a skill that I can take anywhere. I don't have to have my .vimrc file to be powerful. I know how to use the power of the editor.Trouveur
S
1

I have came up with this:

" php remove eol from end of file
autocmd FileType php setlocal noeol binary fileformats="mac,unix,dos"

Thank you all.

Cheers.

Stenophyllous answered 9/11, 2010 at 15:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.