将 ^M (Windows) 换行符转换为普通换行符
Vim 在每一行结尾处显示 ^M
。
如何将其替换为在 Vim 中打开的文件中的正常换行符?
Vim shows ^M
on every line ending.
How do I replace this with a normal line break in a file opened in Vim?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
另外,还有名为 dos2unix 和 unix2dos 的开源实用程序可以完成此操作。 在 Linux 系统上,它们可能是默认安装的; 对于 Windows 系统,您可以从 http://www.bastet.com/ 等下载它们。
Alternatively, there are open-source utilities called dos2unix and unix2dos available that do this very thing. On a linux system they are probably installed by default; for a windows system you can download them from http://www.bastet.com/ amongst others.
其中 ^M 是通过同时按下 3 个键来输入的,ctrl + v + m
where ^M is typed by simultaneously pressing the 3 keys, ctrl + v + m
如果文件是在 Windows 上创建的,请使用 dos2unix 实用程序,
如果文件是在 Mac 上创建的,请使用 mac2unix 实用程序。 :)
use dos2unix utility if the file was created on windows,
use mac2unix utility if the file was created on mac. :)
使用以下命令之一:
或者
Use one of these commands:
Or
在 VIM 的命令模式下:
e ++ff=dos
- 强制以dos
格式打开文件。setl ff=unix
- 将文件转换为unix
格式。up
- 仅在修改后保存文件。In command mode in VIM:
e ++ff=dos
- force open file indos
format.setl ff=unix
- convert file tounix
format.up
- save file only when has been modified.要节省击键次数,您可以避免输入 Ctrl+VCtrl+M,方法是将其放入映射中。 只需打开一个包含 ^M 字符的文件,将其拉出,然后将其粘贴到 .vimrc 中如下所示的行中:
To save keystrokes, you can avoid typing Ctrl+VCtrl+M by placing this in a mapping. Just open a file containing a ^M character, yank it, and paste it into a line like this in your .vimrc:
这对我有用:
This worked for me:
要在 MacOS 上使用
sed
,请执行以下操作:说明:此处的
$'STRING'
语法适用于 bash shell。 Mac 不将\r
视为特殊字符。 通过引用$''
中的命令字符串,您可以告诉 shell 将\r
替换为 ANSI 中指定的实际\r
字符-C标准。To use
sed
on MacOS, do this:Explanation: The
$'STRING'
syntax here pertains to the bash shell. Macs don't treat\r
as special character. By quoting the command string in$''
you're telling the shell to replace\r
with the actual\r
character specified in the ANSI-C standard.这些建议对我来说都不起作用,因为我在使用 vim 和 eclipse 时设法获得了大量
^M
换行符。 我怀疑我遇到了外部案例,但以防万一它对我所做的任何人有帮助。它解决了我的问题
None of these suggestions were working for me having managed to get a load of
^M
line breaks while working with both vim and eclipse. I suspect that I encountered an outside case but in case it helps anyone I did.And it sorted out my problem
如果您使用
Shift+6 Caps+M
键入^M
,它将不会接受。您需要输入
ctrl+v ctrl+m
。If you type
^M
usingShift+6 Caps+M
it won't accept.You need to type
ctrl+v ctrl+m
.^M
会产生不需要的换行符。 为了处理这个问题,我们可以使用 sed 命令,如下所示:^M
gives unwanted line breaks. To handle this we can use thesed
command as follows:只需删除 .vimrc 中的
set binary
即可!Just remove
set binary
in your .vimrc!在 Solaris 上:
即:
这意味着:
On Solaris:
that is:
That means:
命令
其中
表示键入 Ctrl+V,然后键入 Ctrl+ M。解释
替换,% = 所有行
^M 个字符(Ctrl-V 是 Vim 编写 Ctrl ^ 字符的方式,Ctrl-M 在正则表达式后面写入 M,从而产生 ^M 特殊字符)
并换行(
\r
)并在全局范围内执行此操作(而不仅仅是行中的第一次出现)。
Command
Where
<Ctrl-V><Ctrl-M>
means type Ctrl+V then Ctrl+M.Explanation
substitute, % = all lines
^M characters (the Ctrl-V is a Vim way of writing the Ctrl ^ character and Ctrl-M writes the M after the regular expression, resulting to ^M special character)
with new line (
\r
)And do it globally (not just the first occurrence on the line).
在 Linux 和 Mac OS 上,以下操作有效,
其中
^V^M
表示键入 Ctrl+V,然后键入 Ctrl+M。注意:在 Windows 上,您可能需要使用
^Q
而不是^V
,因为默认情况下^V
映射到粘贴文本.On Linux and Mac OS, the following works,
where
^V^M
means type Ctrl+V, then Ctrl+M.Note: on Windows you probably want to use
^Q
instead of^V
, since by default^V
is mapped to paste text.在
vim
中,查看文件格式 — DOS 或 Unix::set filetype=unix
:set fileformat=unix
文件将被写回,不带回车符(CR、^M)。
Within
vim
, look at the file format — DOS or Unix::set filetype=unix
:set fileformat=unix
The file will be written back without carriage return (CR, ^M) characters.
这是唯一对我有用的事情:
找到它: http://vim.wikia.com/wiki/File_format< /a>
This is the only thing that worked for me:
Found it at: http://vim.wikia.com/wiki/File_format
我在 MacVim 中使用 BBEdit 创建的文件显示了一堆
^M
行返回,而不是常规的行返回。 下面的字符串替换解决了这个问题:这很有趣,因为我用相同的字符替换换行符,但我想 Vim 只需要获取一个新的 \r 即可正确显示。 我有兴趣了解其工作原理的基本机制。
A file I had created with BBEdit seen in MacVim was displaying a bunch of
^M
line returns instead of regular ones. The following string replace solved the issue:It's interesting because I'm replacing line breaks with the same character, but I suppose Vim just needs to get a fresh \r to display correctly. I'd be interested to know the underlying mechanics of why this works.
首先,使用
:set ff?
确定您的文件的文件格式。我猜它可能是
unix
,那么问题是您的文件是使用fileformat=dos
创建的,在行尾添加了“^M^J”,但使用读取flieformat=unix
仅从行尾删除“^J”,保留“^M”。只需在 Vim 命令行中输入
:e ++ff=dos
即可将文件格式从 unix 更改为 dos。 它应该可以解决问题。 如果没有,:%s/\r//g
应该可以帮助你。First, use
:set ff?
to figure out the file format your file is.I guess it could be
unix
, then the problem is your file was created withfileformat=dos
adding "^M^J" to the line end but read withflieformat=unix
only removing the "^J" from the line end, leaving the "^M" there.Just input
:e ++ff=dos
in Vim command line to change your file's format from unix to dos. It should solve the problem. If not,:%s/\r//g
should help you out.为了使 ^M 字符匹配,我必须直观地选择它,然后使用操作系统复制到剪贴板命令来检索它。 您可以在尝试替换命令之前通过搜索字符来测试它。
应该选择第一个坏行将
用回车替换所有错误的^M。
这是 MacVim 中的函数,它基于 gvim 7。
编辑:
在我的 Windows 10 机器上再次遇到这个问题,该机器有 Ubuntu for Windows,我认为这个导致 vim 的文件格式问题。 在这种情况下,将 ff 更改为 unix、mac 或 dos 只不过将 ^M 更改为 ^J 并再次更改回来。
这种情况下的解决方案:
我走这条路的原因是因为我想确保我的文件是非破坏性的。 我可以使用
:%s/\r$//g
但这会直接删除回车符,并且可能会产生意想不到的结果。 相反,我们将单个 CR 字符(这里是 ^M 字符)转换为空格,然后删除行末尾的所有空格(无论如何,这对我来说都是一个理想的结果)抱歉重新提出一个早已得到解答的老问题,但似乎存在一些混乱,我想我会帮助澄清其中一些,因为这在谷歌搜索中排名很高。
in order to get the ^M character to match I had to visually select it and then use the OS copy to clipboard command to retrieve it. You can test it by doing a search for the character before trying the replace command.
should select the first bad line
will replace all the errant ^M with carriage returns.
This is as functions in MacVim, which is based on gvim 7.
EDIT:
Having this problem again on my Windows 10 machine, which has Ubuntu for Windows, and I think this is causing fileformat issues for vim. In this case changing the ff to unix, mac, or dos did nothing other than to change the ^M to ^J and back again.
The solution in this case:
The reason I went this route is because I wanted to ensure I was being non-destructive with my file. I could have
:%s/\r$//g
but that would have deleted the carriage returns right out, and could have had unexpected results. Instead we convert the singular CR character, here a ^M character, into a space, and then remove all spaces at the end of lines (which for me is a desirable result regardless)Sorry for reviving an old question that has long since been answered, but there seemed to be some confusion afoot and I thought I'd help clear some of that up since this is coming up high in google searches.
这些都不适合我,所以我尝试了这个,它有效:
输入
:%s/
按 CTRL-VCTRL-M
输入 < code>//g
press Enter
因此,Vim 中的整体命令应类似于
:%s/^M//g
这是做什么的:
:%s
(查找并替换)/^M/
(该符号)/
(无字符)g
(全局)。None of these worked for me, so I tried this, which worked:
type
:%s/
press CTRL-VCTRL-M
type
//g
press Enter
So the overall command in Vim shoud look like
:%s/^M//g
What this does:
:%s
(find and replace)/^M/
(that symbol)/
(with no chars)g
(globally).^M 由 Ctrl+V 和 M 检索,因此也是如此
^M is retrieved by Ctrl+V and M, so do
无需使用 Ctrl:
<代码>:%s/\r$//
Without needing to use Ctrl:
:%s/\r$//
对我有用的简单事情
Simple thing that worked for me
我用 sed 做到了这一点:
sed -i -e 's/\r/\n/g' filename
I did this with sed:
sed -i -e 's/\r/\n/g' filename
怎么样:
<代码>
:%s/\r//g
这对我来说完全有效。
它所做的只是清理所有行的行尾,它删除了 ^M ,仅此而已。
What about just:
:%s/\r//g
That totally worked for me.
What this does is just to clean the end of line of all lines, it removes the ^M and that's it.
这个问题还有很多其他答案,但以下内容仍然最适合我,因为我需要命令行解决方案:
说明:
myfile
: e ++ff=dos
强制重新加载整个文件作为 dos 行结尾。:w ++ff=unix
以使用 unix 行结尾写入文件There are many other answers to this question, but still, the following works best for me, as I needed a command line solution:
Explanation:
myfile
:e ++ff=dos
to force a reload of the entire file as dos line endings.:w ++ff=unix
to write the file using unix line endingsCtrl+M 最小化了我的窗口,但 Ctrl+Enter 实际上插入了一个
^M
性格。 我还必须确保在两次按下之间不要松开 Ctrl 键。所以我的解决方案是:
其中
表示按住 Ctrl,然后按松开V,按下并松开Enter,然后松开Ctrl。如果您正在处理 Windows 生成的文件
上述解决方案将在现有行之间添加额外的行,因为
^M
后面已经有一个不可见的\r
。为了防止这种情况,您需要删除
^M
字符而不替换它们。其中
%
表示“在此缓冲区中”,s
表示“替换”,/
表示“(查找)以下模式”,
指的是获取^M
字符所需按下的按键(见上文),//
表示“什么都没有”(或者,“这两个斜杠之间的模式是空的”),并且g
是一个标志,意思是“全局”,而不是一行中第一次出现。Ctrl+M minimizes my window, but Ctrl+Enter actually inserts a
^M
character. I also had to be sure not to lift off the Ctrl key between presses.So the solution for me was:
Where
<Ctrl-V><Ctrl-Enter>
means to press and hold Ctrl, press and release V, press and release Enter, and then release Ctrl.If you are working on a Windows-generated file
The above solution will add an additional line between existing lines, because there is already an invisible
\r
after the^M
.To prevent this, you want to delete the
^M
characters without replacing them.Where
%
means "in this buffer,"s
means "substitute,"/
means "(find) the following pattern,"<Ctrl-V><Ctrl-Enter>
refers to the keys to press to get the^M
character (see above),//
means "with nothing" (or, "with the pattern between these two slashes, which is empty"), andg
is a flag meaning "globally," as opposed to the first occurrence in a line.这对我有用:
\n
行结尾)所以在vim中:
This worked for me:
\n
line ending)So in vim:
就我而言,
以上都不起作用,我从我的Mac复制了一个CSV文件到Linux机器,我使用了上述所有命令,但没有任何帮助,但下面的命令
我有一个文件,其中^M字符夹在如下所示的行之间
In my case,
Nothing above worked, I had a CSV file copied to Linux machine from my mac and I used all the above commands but nothing helped but the below one
I had a file in which ^M characters were sandwitched between lines something like below