VIM Disable Automatic Newline At End Of File

2020-01-24 01:53发布

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!

13条回答
手持菜刀,她持情操
2楼-- · 2020-01-24 02:10

I've added a tip on the Vim wiki for a similar (though different) problem:

http://vim.wikia.com/wiki/Do_not_auto-add_a_newline_at_EOF

查看更多
萌系小妹纸
3楼-- · 2020-01-24 02:13

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.

查看更多
smile是对你的礼貌
4楼-- · 2020-01-24 02:13

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.

查看更多
Deceive 欺骗
5楼-- · 2020-01-24 02:15

I found this vimscript plugin is helpful for this situation.

Plugin 'vim-scripts/PreserveNoEOL'

Or read more at github

查看更多
Luminary・发光体
6楼-- · 2020-01-24 02:16

Starting with vim v7.4 you can use

:set nofixendofline

There is some information about that change here: http://ftp.vim.org/vim/patches/7.4/7.4.785 .

查看更多
forever°为你锁心
7楼-- · 2020-01-24 02:24

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".

查看更多
登录 后发表回答