Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: Mac vs. Linux ^M Problem

I use Vim and GIT to develop my project on a Linux/Ubuntu machine. In the past, I used to use Windows, which means line ends were DOS format. Now after moving to Linux, I always see ^M confusing/annoying characters at the end of the line. Some people here:

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

suggested that I should make a find/replace or use tools like dos2unix. These are good suggestions, but with a version-controlled project it means I have to add unnecessary commits for the files after change, and it is a big task anyway that is not worth spending time on it. So:

Is there anyway to make Vim tolerant for this? That is, if the file is DOS line-ended, it keeps it like this, and use its formatting for new newlines, and so on.

like image 311
Rafid Avatar asked Sep 15 '26 22:09

Rafid


2 Answers

I have added this line to my .vimrc file.

set fileformats=unix,mac,dos                

If you don't know what a vimrc file is, it's simply the file that configure (and allows you to customize) your vim. By default vim will be looking for a file called .vimrc (notice the dot at the beginning) in the user's home folder. If it cannot find it, it will source it from /etc/vimrc.

like image 169
rahmu Avatar answered Sep 17 '26 12:09

rahmu


Yes, you can set the following in vim to say it's a dos format file:

:set ff=dos

And, likewise:

:set ff=unix
like image 24
Joel Avatar answered Sep 17 '26 12:09

Joel