Convert DOS line endings to Linux line endings in vim

If I open files I created in Windows, the lines all end with ^M .
How do I delete these characters all at once?


dos2unix is a commandline utility that will do this, or :%s/^M//g will if you use Ctrl-v Ctrl-m to input the ^M, or you can :set ff=unix and vim will do it for you.

Docs on the 'fileformat' setting are here, and the vim wiki has a comprehensive page on line ending conversions.

Alternately, if you move files back and forth a lot, you might not want to convert them, but rather to do :set ff=dos , so vim will know it's a DOS file and use DOS conventions for line endings.


Change the lineendings in the view:

:e ++ff=dos
:e ++ff=mac
:e ++ff=unix

This can also be used as saving operation (:w alone will not save using the lineendings you see on screen):

:w ++ff=dos
:w ++ff=mac
:w ++ff=unix

And you can use it from the command-line:

for file in $(ls *cpp)
do 
  vi +':w ++ff=unix' +':q' ${file}
done

I typically use

:%s/r/r/g

which seems a little odd, but works because of the way that vim matches linefeeds. I also find it easier to remember :)

链接地址: http://www.djcxy.com/p/22604.html

上一篇: 在Vim中合并多行(两个块)

下一篇: 在vim中将DOS行结尾转换为Linux行尾