Windows – Vim: Mac with Linux^M issues

Vim: Mac with Linux^M issues… here is a solution to the problem.

Vim: Mac with Linux^M issues

I use Vim and GIT to develop my projects on Linux/Ubuntu machines. I used to use Windows, which means that the line endings were all in DOS format. Now after moving to Linux, I always see ^M confusing/annoying characters at the end of the line. There are some people here:

How to convert the ^M linebreak to ‘normal’ linebreak in a file opened in vim?

Suggested I should do find/replace or use a tool like dos2unix. These are all good suggestions, but for a versioned project, which means I have to add unnecessary commits to the file after the change, it’s a big task that isn’t worth spending time on anyway. So:

Is there any way to make Vim tolerant of this? That is, if the file ends with DOS lines, it stays that way, wraps with its formatting, and so on.

Solution

I’ve added this line to my .vimrc file.

set fileformats=unix,mac,dos                

If you don’t know what a vimrc file is, it’s just a file that configures (and allows you to customize) your vim. By default, vim will look for a file named .vimrc (note the dot at the beginning) in the user’s home folder. If it can’t find it, it will get it from /etc/vimrc.

Related Problems and Solutions