What does ^M character mean in Vim?

2019-01-03 00:22发布

I keep getting ^M character in my vimrc and it breaks my configuration.

标签: unix vim
15条回答
Ridiculous、
2楼-- · 2019-01-03 00:57

You can fix this in vim using

:1,$s/^V^M//g

where ^ is the control character.

查看更多
手持菜刀,她持情操
3楼-- · 2019-01-03 01:02

It probably means you've got carriage returns (different operating systems use different ways of signaling the end of line).

Use dos2unix to fix the files or set the fileformats in vim:

set ffs=unix,dos
查看更多
再贱就再见
4楼-- · 2019-01-03 01:02

try :%s/\^M// At least this worked for me.

查看更多
小情绪 Triste *
5楼-- · 2019-01-03 01:03

To translate the new line instead of removing it:

:%s/\r/\r/g
查看更多
Explosion°爆炸
6楼-- · 2019-01-03 01:05

In Unix it is probably easier to use 'tr' command.

cat file1.txt | tr "\r" "\n" > file2.txt
查看更多
神经病院院长
7楼-- · 2019-01-03 01:08

I got a text file originally generated on a Windows Machine by way of a Mac user and needed to import it into a Linux MySQL DB using the load data command.

Although VIM displayed the '^M' character, none of the above worked for my particular problem, the data would import but was always corrupted in some way. The solution was pretty easy in the end (after much frustration).

Solution: Executing dos2unix TWICE on the same file did the trick! Using the file command shows what is happening along the way.

$ file 'file.txt'
file.txt: ASCII text, with CRLF, CR line terminators

$ dos2unix 'file.txt'
dos2unix: converting file file.txt to UNIX format ...
$ file 'file.txt'
file.txt: ASCII text, with CRLF line terminators

$ dos2unix 'file.txt'
dos2unix: converting file file.txt to UNIX format ...
$ file 'file.txt'
file.txt: ASCII text

And the final version of the file imported perfectly into the database.

查看更多
登录 后发表回答