0

去掉 vim 每行结尾的 ^M

 3 years ago
source link: https://zhiqiang.org/it/remove-m-from-vim.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

去掉 vim 每行结尾的 ^M

作者: 张志强

, 发表于 2020-02-27

, 共 378 字 , 共阅读 397 次

有时候用vim打开文件,每行结尾都有一个灰色的^M。这个原因是该文件在 windows 系统上被创建。windows 的换行符是\n\r,而 unix 下的文本换行只需要\n,这个多余的\r就被显示为^M,虽然显示为两个字符,但其实是一个字符。

大多数情况下,打开这种文件,vim的状态栏会显示文件格式:utf-8[dos],此时只需要下面命令可转为unix格式,即可删除所有的^M:

:set ff=unix

如果vim显示文件格式已经是utf-8[unix],这时候上述命令就不管用了,说明 vim 识别类型错误,可以先把它纠正(即用dos格式打开当前文件),再变更类型:

:e ++ff=dos 
:set ff=unix

还有一种方法是字符串替换:

:%s/\r//g

注意这里是用\r而不是^M,这也是很多人不会删除^M的原因。

Q. E. D.

avatar-0.jpg
avatar-2.jpg
无名2145
2020-10-13

git时候这个是个灾难

avatar-1.jpg
2020-03-03

赞。 自己有时候用:%s/Ctrl-vCtrl-M//g


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK