In vim, how does one remove the eol of the last line of the file?

Source: Internet
Author: User
Tags eol

In other words, no eol at the end of a file is an incorrect file format, but many software in windows like this. We will not discuss this for the moment. Let's first look at how eol needs to be removed:

If the PHP file ends with?>, And this php has been included, and... (I forgot this condition. In short, I have never met or want to touch it), So?> The linefeed is output to the page.

In fact, this problem is a good solution. Can PHP files be used?> At the end, it is officially recommended. However, what do people want you to do?> End with no eol. What should I do?

Below are some solutions I have found:

Method 1:

autocmd BufWritePre  *.php setlocal binary autocmd BufWritePre  *.php setlocal noeol autocmd BufWritePost *.php setlocal nobinary 

Disadvantage. After bin is set, files are automatically saved in unix format, which may cause a lot of trouble. For example, svn submission may conflict with others. svn conflicts can also be solved by svn, but it is not in the scope of this article.

Method 2:

fun! a:remove_eol()    silent !~/bin/noeol <afile>endfunautocmd BufWritePost *.php call a:remove_eol()

In fact, I wrote a small tool to remove eol. Disadvantages: the unix environment is required. In normal windows, it is not that convenient for you to create a script on it.

Method 3:

See http://vim.wikia.com/wiki/Preserve_missing_end-of-line_at_end_of_text_files

However, this method is not easy to use in newer vim versions. You need to change it. I will post the modified method:

" Preserve noeol (missing trailing eol) when saving file. In order" to do this we need to temporarily 'set binary' for the duration of" file writing, and for DOS line endings, add the CRs manually." For Mac line endings, also must join everything to one line since it doesn't" use a LF character anywhere and 'binary' writes everything as if it were Unix." This works because 'eol' is set properly no matter what file format is used," even if it is only used when 'binary' is set.augroup automatic_noeolau!autocmd BufRead *.php setlocal noeol au BufWritePre  *.php call TempSetBinaryForNoeol()au BufWritePost *.php call TempRestoreBinaryForNoeol()fun! TempSetBinaryForNoeol()  let s:save_binary = &binary  if ! &eol && ! &binary    setlocal binary    if &ff == "dos" || &ff == "mac"      undojoin | silent 1,$-1s#$#\^M    endif    if &ff == "mac"      let s:save_eol = &eol      undojoin | %join!      " mac format does not use a \n anywhere, so don't add one when writing in      " binary (uses unix format always)      setlocal noeol    endif  endifendfunfun! TempRestoreBinaryForNoeol()  if ! &eol && ! s:save_binary    if &ff == "dos"      undojoin | silent 1,$-1s/\r$/    elseif &ff == "mac"      undojoin | %s/\r/\r/g      let &l:eol = s:save_eol    endif    setlocal nobinary  endifendfunaugroup END

The difference between the original version and the version is in the ^ M place. If you have vim on hand, you can try: % s/$/^ M. If you do not use it, it will become a blank line. Help: replace. You can see that a slash is required for the new version.

(Note: <Ctrl + V> <Ctrl + M>)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.