Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make vim :source accept different line endings?

Using vim's :source command on a vimscript file with dos line endings is giving me errors that it can't deal with ^M characters.

The ^M characters are part of dos line endings, but not unix line endings. So it is trying to :source the file using a unix file format.

The thing is, I have :set fileformats=unix,dos setting on. When opening and editing the file (not :source: -ing) there are no line ending problems. Vim sees a file with pure dos line endings and as per fileformats it adjusts itself accordingly.

:help fileformats only gives the barest clue that on dos systems if you have :set fileformats=unix,dos, vim performs an ad-hoc detection of which it should use (and as an irrelevant detail, this :source detection treats mixed line endings differently than than for opening files).

I'm on a modern mac system, how do I get it to :source a dos line ending file?

like image 316
Ein Avatar asked Dec 20 '12 18:12

Ein


1 Answers

This is discussed at :h :source_crnl.

On UNIX systems, which includes Mac OS X, there is no automatic CRLF detection, and an actual CR at the end of a line will may raise an error, e.g. in a mapping. From the help:

On other systems, Vim expects ":source"ed files to end in a <NL>. These always work. If you are using a file with <CR><NL> <EOL>s (for example, a file made on MS-DOS), all lines will have a trailing <CR>.

For best compatibility, it is best to have Vim script files always use NL newlines. These will always work everywhere, provided that the first line of the script doesn't for some reason end in a CR and 'fileformats' is not empty (it isn't empty by default).

In short, consider converting your line endings to LF.

like image 95
glts Avatar answered Oct 21 '22 02:10

glts