There's little one can do about it how about suppressing the error via :silent! undojoin, so that we still get the benefit of :undojoin in most cases, and never cause an error?! - Inkarkat 12:49, Novem(UTC) Regarding the :undojoin error: I think this is caused by a user undo operation immediately before the :write command. It seems that the switch from the :substitute to :normal did it. The PreserveNoEOL plugin uses the above pure Vimscript implementation to automatically preserve no EOL on all or certain opened files, but it also comes with alternative strategies that use postprocessing in Python or Perl to remove the trailing EOL character after the save, which is probably more robust.Ĭomments Thanks Fritzophrenic for looking into this so quickly! Yes, your updated script works for me (for all three fileformats).
" Suppress them for now.if you can figure out and fix them instead, " please update silent ! undojoin | silent %s /\r/\ r/geĮndif setlocal nobinary call winrestview (s:save_view ) endif endfunction Related scripts " Suppress them for now.if you can figure out and fix them instead, " please update silent ! undojoin | silent 1 ,$ -1s /\r$// e endif elseif & ff = "mac" " Sometimes undojoin gives errors here, even when it shouldn't. If & ff = "dos" if line ( '$' ) > 1 " Sometimes undojoin gives errors here, even when it shouldn't.
endif endif endfunction function ! s:TempRestoreBinaryForNoeol () if ! & eol & ! s:save_binary However, inside the outer " if statement, we already know that 'noeol' is set, so no special logic " is needed. " This works because 'eol' is set properly no matter what file format is used, " even if it is only used when 'binary' is set.Īu ! au BufWritePre * call TempSetBinaryForNoeol () au BufWritePost * call TempRestoreBinaryForNoeol ()įunction ! s:TempSetBinaryForNoeol () let s:save_binary = & binary if ! & eol & ! & binary let s:save_view = winsaveview () setlocal binary if & ff = "dos" || & ff = "mac" if line ( '$' ) > 1 undojoin | exec "silent 1,$-1normal! A\\" endif endif if & ff = "mac" undojoin | % join ! " mac format does not use a \n anywhere, so we don't add one when writing " in binary (which uses unix format always).
" 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. 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. " Preserve noeol (missing trailing eol) when saving file. :undojoin is used to keep these temporary line ending transformations transparent to the user. So the rest of the script manually adds the correct line endings for the current format, and removes them again after writing. Unfortunately, when 'binary' is set, the file is always written with Unix-format line endings. So, the core of the script simply sets 'binary' temporarily. Even though 'eol' is set properly, it only has any effects when 'binary' is also set. The script relies on Vim's recognition of a missing line ending, saved in the 'eol' option value. This works for all three line ending styles which Vim recognizes: DOS (Windows), Unix, and traditional Mac.
Some (arguably broken) Windows apps like this missing EOL, so this might be useful for Windows. If it was read without one, it will be saved without one. If the file was read with EOL at the end, it will be saved with one.
This script causes Vim to 'preserve' a missing end-of-line at the end of a text file when you save it, if it was missing when the file was read. This script becomes uneccessary for Vim 7.4.785 and later. Vim 7.4.785 adds the 'fixeol' option that can be disabled to automatically preserve any missing EOL at the end of the file. I would describe the process in more detail, but I only have 10.4, and I know Terminal was changed a lot in 10.5 and later.This tip is deprecated for the following reasons: The basic idea is that you want to have Terminal send ESC[1~ instead of ESC[H for Home and ESC[4~ instead of ESC[F for End (or Shift-Home/Shift-End if you want to keep Home/End for accessing scrollback).
A search would probably turn up some places that describe how to do it ( here is one). Otherwise, you might try adjusting the codes that Terminal sends when you use Home and End (unshifted even, if you like).
vimrc: :" map Mac OS X Terminal.app default Home and End If you want to stick with the default bindings (and having to use the Shifted variants in Terminal), you might try putting something like this in your. The default bindings for Home and End are a bit different from what a program would expect based on the usual TERM setting (i.e. This answer assumes you are using vim in a Terminal window.