3

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.

Community
  • 1
  • 1
Rafid
  • 18,991
  • 23
  • 72
  • 108
  • 2
    With subversion you can set a property `svn:eol-style=native` that will cause it to convert line endings to.. native type for all platforms automatically. I don't know if git has that feature, but you might look into that first. It probably does. – Keith Jan 08 '11 at 11:26
  • Hmm... that's a good suggestion. I should try it. One vote from me! – Rafid Jan 08 '11 at 11:29
  • http://www.kernel.org/pub/software/scm/git/docs/gitattributes.html :-) – Keith Jan 08 '11 at 13:59
  • Thanks Keith. Could you please put your comments as an organized answer? This way I can mark it as the accepted answer. – Rafid Jan 08 '11 at 15:07
  • Are you the only one or one of only a handful of people working on that repository? If yes, then I would use [`git filter-branch`](http://www.kernel.org/pub/software/scm/git/docs/git-filter-branch.html) to rewrite the project history, retroactively converting all the files to Unix-style line endings throughout history. That way you avoid ever having to deal with the issue again. – Aristotle Pagaltzis Jan 09 '11 at 16:59
  • @Aristotle, that would be great! Unfortunately, I am not the only one using the repository. So why can't I do the same if there are other users? What will happen to them? – Rafid Jan 09 '11 at 17:35
  • 1
    Rewriting history causes all commit IDs to change (because the content of the commits changes), so it looks like all the history is now in another branch. If the others have unpushed commits, they will need to carry them over to the rewritten timeline; [I have posted the recipe for this](http://stackoverflow.com/questions/4084868). For less skilled git users you could say they must push everything at the end of day X, then you do the rewrite and push it, and the next morning they have to make a new clone. You will just need to coordinate *somehow*, which is not practical if the team is huge. – Aristotle Pagaltzis Jan 10 '11 at 01:19

3 Answers3

2

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.

rahmu
  • 5,708
  • 9
  • 39
  • 57
  • How this different than @Joel's answer? – Rafid Jan 08 '11 at 15:08
  • It does it automatically every time you load a file in Vim. You won't have to input it for each file. Plus it deals with the 3 types of carriage returns (unix, mac and dos ... duh :P ) – rahmu Jan 09 '11 at 01:41
  • I see, yes, that's a good point. Unfortunately, it doesn't seem to be working with me. I do that and I still see many ^M in my file. – Rafid Jan 09 '11 at 09:33
  • OFF Topic (I guess) - see this --> http://sites.google.com/site/bemylifeeasy/Home/codeconvert – SergioAraujo Jan 09 '11 at 16:31
1

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

:set ff=dos

And, likewise:

:set ff=unix
Joel
  • 29,538
  • 35
  • 110
  • 138
  • I tried it and it doesn't work. Besides that, I want it to automatically do this according to the format of the file, rather than having to do it every time I open a new file, not to mention the confusion that it would cause if I forget to return it back to Linux. – Rafid Jan 08 '11 at 11:15
1

The best answer I found is @Keith answer in the comments:

"With subversion you can set a property svn:eol-style=native that will cause it to convert line endings to.. native type for all platforms automatically. I don't know if git has that feature, but you might look into that first. It probably does."

And these links might be useful link is how to set:
http://help.github.com/dealing-with-lineendings/
http://git-scm.com/docs/gitattributes

Rafid
  • 18,991
  • 23
  • 72
  • 108