Question

I often have to edit script files, the interpreter for which treats files that have an EOL marker on the last line of the file as an error (i.e. the file is treating CRLF as "newlines", not as "line endings").

Currently, I open these files in Vim using binary mode (-b on the command line). It autodetects the lack of EOL on the final line and sets the "noeol" option appropriately, which prevents it from writing an EOL on the last line.

Because the file has CRLF line endings, I get lots of ^Ms at the end of my lines (because it interprets only Unix-style line endings in binary mode, it seems). I can't open it in text mode because the "noeol" option is ignored for non-binary files.

This is very annoying, and I always have to remember to manually type the ^M at the end of each line! Is there some way I can force it to accept DOS-style line endings in binary mode, or force it to listen to the EOL option in text mode?

Was it helpful?

Solution

can you run the dos2unix command before editing?

OTHER TIPS

Yes, you shouldn't need to manually enter a ^M on every line; that would be tedious! Try entering this:

:set ff=dos

You may also find this article useful: change end-of-line format for dos-unix

On further review, a global search and replace is probably neccessary, so give this a shot. (I tested this out earlier today and it worked on a unix file being edited with gvim in Windows XP, and not in binary mode.)

:%s/^M//g

Type that command exactly as shown except for the ^M, which is a special character. The way you produce it is to press Ctrl and v together then hit Enter (let up on CRTL+V prior to hitting enter)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top