Gene Kwiecinski wrote:
"fileformats=dos,unix", so both formats are available, yet the
detection and switching does not seem to work.

Are you sure _every_ line ends in "^M"?

Positive. Every single line shows an ^M at the end. "set fileformat"
gives "unix" after loading. Setting fileformat to "dos" doesn't change
the files interpretation in vim. Somehow I think I miss something.

Uhhh, don't think it *should* automagically delete the ^Ms.  I'm always
running into that, and in addition to an almost reflexive alt-EIFD to go
dos-mode, I *still* always have to ':s/^V^M' to get rid of 'em, and I'm
using version 6.4, not even 7.x, so it's definitely been around for a
while.



Setting ff=dos after the file is loaded changes nothing to the contents of the file data in memory, it only changes how the ends-of-lines will be represented on output. So if you have a file with mixed ends-of-lines and 'fileformat' detected as "unix", using ":setlocal ff=dos" will result in some lines having CR+LF (where there was only LF before) and others (which already had CR+LF shown as ^M at end of line) will get CR+CR+LF. So this is not the way to go.

To detect a file with mixed ends-of-lines as dos-format, we must first disable automatic "unix" filetype detection. One way to do this is to set the 'fileformats' (plural) option to the empty string:

        :set fileformats=

Now we can manually detect the file's format (see ":help ++opt"):

        :e ++ff=dos filename

which will interpret CR+LF as an end-of-line, and LF-without-CR also as an end-of-line. So now all lines (in memory) are as they should be, with no spurious end-of-line ^M showing. ^M will only appear if there is a CR not immediately followed by a LF (either a lone CR in the middle, or more than one CR before a LF).

We must now save this "file-in-memory" to disk, to make the "repairs" permanent. Here we have the choice of how we want to save it:

- To save the file with CR+LF at the end of every line, leave 'fileformat' at its current value ("dos") and just write the file:

        :w

- To save the file with LF-only:

        :setlocal ff=unix
        :w

- To save the file with CR-only:

        :setlocal ff=mac
        :w

After that, we may want to re-enable automatic fileformat detection: either set it back to its default:

        :set fileformats&

or set the value explicitly, for example:

        :set ffs=dos,unix,mac



Best regards,
Tony.
--
hundred-and-one symptoms of being an internet addict:
238. You think faxes are old-fashioned.

Reply via email to