views:

390

answers:

2

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?

A: 

can you run the dos2unix command before editing?

Winter
I thought this would destroy the file, but it seems to work just fine. It's not an ideal solution at all, and I was holding out for a better answer, but I guess one isn't coming... I'll accept this one, but I might choose a better answer if it comes along.
rmeador
A: 

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)

Drew
I'm aware of setting the file format to DOS mode for text files. It doesn't seem to work in binary mode. I tried using that command both after the file is opened and using --cmd on the command line. Neither had any effect.
rmeador