On 08/09/11 07:50, wombatvvv wrote:
Hi,

I find if I use VIM in Windows, save the file and then open it in Linux, I
got a whole heap of errors. It's something to do with the end-of-line
character. In Linux it's detected as ^M and causes errors in .vim scripts.

How do I get my Windows VIM to play nice with my Linux VIM?

Thanks.

For ordinary files (anything except Vim scripts i.e. *vimrc or *.vim), if you edit your Windows files with Vim, and if your Unix Vim has both "dos" and "unix" in its 'fileformats' setting (plural), there ought to be no problem. For Vim scripts, the following is still relevant but the most important is the last section of this reply.

A problem may arise if you edit your files on Windows with some braindead editor which doesn't understand that and end-of-line is there to *end* a line, not to *separate* lines, and which doesn't put an EOL sequence on the last line. When Unix Vim reads that file, it notices the missing EOL, adds a Unix-like EOL (LF only) at the very end, and then the 'fileformats' EOL-checking heuristic kicks in, notices that there is one line (the last one) which has LF-without-CR, and refuses to consider the file as a DOS-format file. Hence the ^M (carriage-returns) at the end of every line except the last.

There are two possible workarounds:

Method 1: In your breaindead Windows editor, make sure that there is an empty line at the end of the file. This ensures that the very last item in the file is a (DOS-like) end-of-line sequence.

Method 2: In your Unix Vim, open your suspect Windows files with

        :e ++ff=dos filename.ext

This will override the 'fileformats' heuristic, and accept either a CRLF pair or a lone LF as an end-of-line when reading. It will also set 'fileformat' (singular) for that file to "dos". When writing that file, Vim will write proper DOS-like CRLF pairs at the end of every line including the last, and that particular file will thereafter be recognizable as a DOS file with no nudging necessary.


For Vim scripts (which will be sourced by Vim), there is an additional hitch: Vim for Windows will accept scripts with either Unix or DOS ends-of-lines, but Unix Vim requires Unix ends-of-lines only. To make sure that a script (or any file) has Unix EOLs, use

        :setlocal fileformat=unix

before you write it. (Note: :w =+ff=unix will write the file _this time_ with Unix-like EOLs but it won't remember that the file needs to _always_ be written Unix-like.)



Best regards,
Tony.
--
Genius may have its limitations, but stupidity is not thus
handicapped.
                -- Elbert Hubbard

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

Reply via email to