Vim: Mac vs. Linux ^M Problem

回眸只為那壹抹淺笑 提交于 2020-01-23 09:55:44

问题


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.


回答1:


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.




回答2:


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

:set ff=dos

And, likewise:

:set ff=unix



回答3:


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



来源:https://stackoverflow.com/questions/4633564/vim-mac-vs-linux-m-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!