Vim no end of line on last line or eof

核能气质少年 提交于 2019-12-20 08:58:14

问题


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.


回答1:


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.




回答2:


This is even better i found somewhere:

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



回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/4133501/vim-no-end-of-line-on-last-line-or-eof

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!