简体   繁体   English

Vim 中的 ^M 字符是什么意思?

[英]What does ^M character mean in Vim?

I keep getting ^M character in my vimrc and it breaks my configuration.我在 vimrc 中不断收到^M字符,它破坏了我的配置。

Unix uses 0xA for a newline character. Unix 使用 0xA 作为换行符。 Windows uses a combination of two characters: 0xD 0xA. Windows 使用两个字符的组合:0xD 0xA。 0xD is the carriage return character. 0xD 是回车符。 ^M happens to be the way vim displays 0xD (0x0D = 13, M is the 13th letter in the English alphabet). ^M恰好是 vim 显示 0xD 的方式(0x0D = 13,M 是英文字母表中的第 13 个字母)。

You can remove all the ^M characters by running the following:您可以通过运行以下命令删除所有^M字符:

:%s/^M//g

Where ^M is entered by holding down Ctrl and typing v followed by m , and then releasing Ctrl .其中^M是通过按住Ctrl并键入v后跟m ,然后释放Ctrl来输入的。 This is sometimes abbreviated as ^V^M , but note that you must enter it as described in the previous sentence, rather than typing it out literally.这有时缩写为^V^M ,但请注意,您必须按照前一句中的说明输入它,而不是按字面输入。

This expression will replace all occurrences of ^M with the empty string (ie nothing).此表达式将所有出现的^M替换为空字符串(即什么都没有)。 I use this to get rid of ^M in files copied from Windows to Unix (Solaris, Linux, OSX).我用它来删除从 Windows 复制到 Unix(Solaris、Linux、OSX)的文件中的^M

:%s/\r//g 

worked for me today.今天为我工作。 But my situation may have been slightly different.但我的情况可能略有不同。

To translate the new line instead of removing it:要翻译新行而不是删除它:

:%s/\r/\r/g

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:使用dos2unix修复文件或设置 vim 中的文件格式:

set ffs=unix,dos

Let's say your text file is - file.txt, then run this command -假设您的文本文件是-file.txt,然后运行此命令-

dos2unix file.txt 

It converts the text file from dos to unix format.它将文本文件从 dos 转换为 unix 格式。

I removed them all with sed:我用 sed 将它们全部删除:

sed -i -e 's/\r//g' <filename>

Could also replace with a different string or character.也可以用不同的字符串或字符替换。 If there aren't line breaks already for example you can turn \r into \n :例如,如果没有换行符,您可以将\r转换为\n

sed -i -e 's/\r/\n/g' <filename>

Those sed commands work on the GNU/Linux version of sed but may need tweaking on BSDs (including macOS).这些sed命令适用于sed的 GNU/Linux 版本,但可能需要在 BSD(包括 macOS)上进行调整。

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.我通过 Mac 用户获得了最初在 Windows 机器上生成的文本文件,需要使用load data命令将其导入 Linux MySQL DB。

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.尽管 VIM 显示了 '^M' 字符,但上述方法均不适用于我的特定问题,数据会导入但总是以某种方式损坏。 The solution was pretty easy in the end (after much frustration).最终解决方案非常简单(经过多次挫折)。

Solution: Executing dos2unix TWICE on the same file did the trick!解决方案:在同一个文件上执行dos2unix TWICE就可以了! Using the file command shows what is happening along the way.使用file命令可以显示一路上发生的事情。

$ 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.文件的最终版本完美地导入数据库。

In Unix it is probably easier to use 'tr' command.在 Unix 中,使用“tr”命令可能更容易。

cat file1.txt | tr "\r" "\n" > file2.txt

This is the only thing that worked in my case:这是在我的情况下唯一有效的方法:

:e ++ff=dos

:wq

You can fix this in vim using您可以使用 vim 修复此问题

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

where ^ is the control character.其中 ^ 是控制字符。

In FreeBSD, you can clear the ^M manually by typing the following:在 FreeBSD 中,您可以通过键入以下内容手动清除^M

:%s/ Ctrl + V , then Ctrl + M , then Ctrl + M again. :%s/ Ctrl + V ,然后是Ctrl + M ,然后是Ctrl + M

I've discovered that I've been polluting files for weeks due to the fact that my Homebrew Mvim instance was set to use filetype=dos.我发现由于我的 Homebrew Mvim 实例设置为使用 filetype=dos,我已经污染了数周的文件。 Made the required change in.vimrc....在.vimrc 中进行了所需的更改......

If you didn't specify a different fileformat intentionally (say, :e ++ff=unix for a Windows file), it's likely that the target file has mixed EOLs.如果您没有有意指定不同的文件格式(例如,对于fileformat文件, :e ++ff=unix ),则目标文件可能混合了 EOL。

For example, if a file has some lines with <CR><NL> endings and others with <NL> endings, and fileformat is set to unix automatically by Vim when reading it, ^M (<CR>) will appear.例如,如果一个文件有一些以<CR><NL>结尾的行和其他以<NL>结尾的行,并且fileformatunix自动设置为 unix 读取它时,会出现^M (<CR>) In such cases, fileformats (note: there's an extra s ) comes into play.在这种情况下, fileformats (注意:有一个额外s )开始发挥作用。 See :help ffs for the details.有关详细信息,请参阅:help ffs

If it breaks your configuration, and the ^M characters are required in mappings, you can simply replace the ^M characters by <Enter> or even <Cm> (both typed as simple character sequences, so 7 and 5 characters, respectively).如果它破坏了您的配置,并且映射中需要 ^M 字符,您可以简单地用<Enter>甚至<Cm>替换 ^M 字符(两者都输入为简单的字符序列,因此分别为 7 和 5 个字符)。

This is the single recommended, portable way of storing special keycodes in mappings这是在映射中存储特殊键码的唯一推荐的、可移植的方式

try :%s/\^M// At least this worked for me.试试:%s/\^M//至少这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM