0
去掉 vim 每行结尾的 ^M
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
有时候用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.
无名2145
2020-10-13
git时候这个是个灾难
2020-03-03
赞。 自己有时候用:%s/Ctrl-vCtrl-M//g
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK