I'm using Vim 8.2 to edit some text files for a very old MSX-DOS system; these have a Ctrl-Z (0x1A) character after the final CR LF to indicate the end of the file, as is usual with CP/M and similar disk operating systems.
I normally edit with fileformats=unix
and nofixendofline
and in this case I see, after the last line of text and trailing <0d>
, a line with the Ctrl-Z character, displayed as <1a>
. I can edit and save and, so long as I manually add the CR at the end of any new lines I add, all is fine.
When I switch to DOS fileformat and re-read the file (:set fileformats=dos | e
) the <0d>
characters disappear (as expected), and so does the final <1a>
character. But unlike the CRs, the Ctrl-Z is not added back in when I write out the file. (I'm writing it immediately after the above command, without changing anything.)
Is there a way when using fileformat=dos
to get Vim to keep the Ctrl-Z at EOF? I searched the help brought up by :help fileformats
(the help file is options.txt
) which says:
When "dos" is used, CTRL-Z at the end of a file is ignored.
See |file-formats| and |file-read|.
*file-read*
(in insert.txt
) says the same.
fileformats
is global option used for auto-detection. It defaults to unix,dos and should never be touched. If ever need to force format only set the localfileformat
. E.g.e ++ff=dos
unix,dos
on every platform. And if it really never should be touched, it would not be settable, much less have options that are not in the default list. ¶ I happen prefer always initially to use Unix format, even on Windows, and even for DOS files (because I don't like Vim silently deleting characters from files as I load them, as well as because it serves as a good, obvious warning that I'm editing DOS-format files). Thus I havefileformats=unix
as my default, and I switch tofileformats=unix,dos
and re-read the file when I wish to enable DOS-mode editing.set ffs=unix,dos
once in vimrc 2. Settingffs=unix
breaks auto-detection, so it's a wrong thing to do. 3. As "unix" precedes "dos" in ffs, the default for new files will be "unix" anyway. 4. Then manually setting fileformat would only be required one out of 100 times if auto-detection ever fails.