Convert DOS line endings to Linux line endings in

2019-01-01 09:30发布

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

26条回答
姐姐魅力值爆表
2楼-- · 2019-01-01 10:06

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 :)

查看更多
素衣白纱
3楼-- · 2019-01-01 10:07

You can use:

vim somefile.txt +"%s/\r/\r/g" +wq

or dos2unix utility .

查看更多
初与友歌
4楼-- · 2019-01-01 10:09

I found a very easy way: Open the file with nano: nano file.txt

Press CTRL+O to save, but before pressing Enter, press: ALT+D to toggle betwen DOS and Unix/Linux line-endings, or: ALT+M to toggle betwen Mac and Unix/Linux line-endings then press Enter to save and CTRL+X to quit.

查看更多
呛了眼睛熬了心
5楼-- · 2019-01-01 10:09

You can use the following command:
:%s/^V^M//g
where the '^' means use CTRL key.

查看更多
素衣白纱
6楼-- · 2019-01-01 10:09

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.

查看更多
妖精总统
7楼-- · 2019-01-01 10:12
:g/Ctrl-v Ctrl-m/s///

CtrlM is the character \r, or carriage return, which DOS line endings add. CtrlV tells vim to insert a literal CtrlM character at the command line.

Taken as a whole, this command replaces all \r with nothing, removing them from the ends of lines.

查看更多
登录 后发表回答