将 ^M (Windows) 换行符转换为普通换行符

发布于 2024-07-19 03:42:28 字数 74 浏览 14 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

轻拂→两袖风尘 2024-07-26 03:42:29

另外,还有名为 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.

っ左 2024-07-26 03:42:29
sed s/^M//g file1.txt > file2.txt

其中 ^M 是通过同时按下 3 个键来输入的,ctrl + v + m

sed s/^M//g file1.txt > file2.txt

where ^M is typed by simultaneously pressing the 3 keys, ctrl + v + m

梦境 2024-07-26 03:42:29

如果文件是在 Windows 上创建的,请使用 dos2unix 实用程序,
如果文件是在 Mac 上创建的,请使用 mac2unix 实用程序。 :)

use dos2unix utility if the file was created on windows,
use mac2unix utility if the file was created on mac. :)

葬花如无物 2024-07-26 03:42:29

使用以下命令之一:

:%s/\r//g

或者

:%s/\r\(\n\)/\1/g

Use one of these commands:

:%s/\r//g

Or

:%s/\r\(\n\)/\1/g
陌路终见情 2024-07-26 03:42:29

在 VIM 的命令模式下:

:e ++ff=dos | setl ff=unix | up

e ++ff=dos - 强制以 dos 格式打开文件。

setl ff=unix - 将文件转换为 unix 格式。

up - 仅在修改后保存文件。

In command mode in VIM:

:e ++ff=dos | setl ff=unix | up

e ++ff=dos - force open file in dos format.

setl ff=unix - convert file to unix format.

up - save file only when has been modified.

戏舞 2024-07-26 03:42:29

节省击键次数,您可以避免输入 Ctrl+VCtrl+M,方法是将其放入映射中。 只需打开一个包含 ^M 字符的文件,将其拉出,然后将其粘贴到 .vimrc 中如下所示的行中:

nnoremap <Leader>d :%s/^M//g<CR>

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:

nnoremap <Leader>d :%s/^M//g<CR>
孤独陪着我 2024-07-26 03:42:29

这对我有用:

:% s/\r\n/\r

This worked for me:

:% s/\r\n/\r
追风人 2024-07-26 03:42:29

要在 MacOS 上使用 sed,请执行以下操作:

sed -i.bak 

说明:此处的 $'STRING' 语法适用于 bash shell。 Mac 不将 \r 视为特殊字符。 通过引用 $'' 中的命令字符串,您可以告诉 shell 将 \r 替换为 ANSI 中指定的实际 \r 字符-C标准。

s/\r//' <filename>

说明:此处的 $'STRING' 语法适用于 bash shell。 Mac 不将 \r 视为特殊字符。 通过引用 $'' 中的命令字符串,您可以告诉 shell 将 \r 替换为 ANSI 中指定的实际 \r 字符-C标准。

To use sed on MacOS, do this:

sed -i.bak 

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.

s/\r//' <filename>

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.

说不完的你爱 2024-07-26 03:42:29

这些建议对我来说都不起作用,因为我在使用 vim 和 eclipse 时设法获得了大量 ^M 换行符。 我怀疑我遇到了外部案例,但以防万一它对我所做的任何人有帮助。

:%s/.$//g

它解决了我的问题

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.

:%s/.$//g

And it sorted out my problem

半﹌身腐败 2024-07-26 03:42:29
:g/^M/s// /g

如果您使用 Shift+6 Caps+M 键入 ^M,它将不会接受。

您需要输入ctrl+v ctrl+m

:g/^M/s// /g

If you type ^M using Shift+6 Caps+M it won't accept.

You need to type ctrl+v ctrl+m.

留蓝 2024-07-26 03:42:29

^M 会产生不需要的换行符。 为了处理这个问题,我们可以使用 sed 命令,如下所示:

sed 's/\r//g'

^M gives unwanted line breaks. To handle this we can use the sed command as follows:

sed 's/\r//g'
死开点丶别碍眼 2024-07-26 03:42:29

只需删除 .vimrc 中的set binary即可!

Just removeset binary in your .vimrc!

旧伤还要旧人安 2024-07-26 03:42:29

在 Solaris 上:

:%s///g

即:

:%s/^M //g

这意味着:

  • % = 所有行,
  • s = 替换,
  • ^M = 您想要替换的内容
  • // = 不替换
  • g = 全局(不仅仅是第一次出现)

On Solaris:

:%s/<CTRL+V><CTRL+M>//g

that is:

:%s/^M//g

That means:

  • % = all lines,
  • s = substitute,
  • ^M = what you desire to substitute
  • // = replace with nothing
  • g = globally (not only the first occurrance)
喜爱皱眉﹌ 2024-07-26 03:42:28

命令

:%s/<Ctrl-V><Ctrl-M>/\r/g

其中 表示键入 Ctrl+V,然后键入 Ctrl+ M

解释

:%s

替换,% = 所有行

<Ctrl-V><Ctrl-M>

^M 个字符(Ctrl-V 是 Vim 编写 Ctrl ^ 字符的方式,Ctrl-M 在正则表达式后面写入 M,从而产生 ^M 特殊字符)

/\r/

并换行(\r)

g

并在全局范围内执行此操作(而不仅仅是行中的第一次出现)。

Command

:%s/<Ctrl-V><Ctrl-M>/\r/g

Where <Ctrl-V><Ctrl-M> means type Ctrl+V then Ctrl+M.

Explanation

:%s

substitute, % = all lines

<Ctrl-V><Ctrl-M>

^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)

/\r/

with new line (\r)

g

And do it globally (not just the first occurrence on the line).

懵少女 2024-07-26 03:42:28

在 Linux 和 Mac OS 上,以下操作有效,

:%s/^V^M/^V^M/g

其中 ^V^M 表示键入 Ctrl+V,然后键入 Ctrl+M

注意:在 Windows 上,您可能需要使用 ^Q 而不是 ^V,因为默认情况下 ^V 映射到粘贴文本.

On Linux and Mac OS, the following works,

:%s/^V^M/^V^M/g

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.

你的往事 2024-07-26 03:42:28

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.

心是晴朗的。 2024-07-26 03:42:28

这是唯一对我有用的事情:

:e ++ff=dos

找到它: http://vim.wikia.com/wiki/File_format< /a>

This is the only thing that worked for me:

:e ++ff=dos

Found it at: http://vim.wikia.com/wiki/File_format

变身佩奇 2024-07-26 03:42:28

我在 MacVim 中使用 BBEdit 创建的文件显示了一堆 ^M 行返回,而不是常规的行返回。 下面的字符串替换解决了这个问题:

:%s/\r/\r/g

这很有趣,因为我用相同的字符替换换行符,但我想 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:

:%s/\r/\r/g

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.

小巷里的女流氓 2024-07-26 03:42:28

首先,使用 :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 with fileformat=dos adding "^M^J" to the line end but read with flieformat=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.

肤浅与狂妄 2024-07-26 03:42:28

为了使 ^M 字符匹配,我必须直观地选择它,然后使用操作系统复制到剪贴板命令来检索它。 您可以在尝试替换命令之前通过搜索字符来测试它。

/^M

应该选择第一个坏行将

:%s/^M/\r/g

用回车替换所有错误的^M。

这是 MacVim 中的函数,它基于 gvim 7。

编辑:

在我的 Windows 10 机器上再次遇到这个问题,该机器有 Ubuntu for Windows,我认为这个导致 vim 的文件格式问题。 在这种情况下,将 ff 更改为 unix、mac 或 dos 只不过将 ^M 更改为 ^J 并再次更改回来。

这种情况下的解决方案:

:%s/\r$/ /g
:%s/ $//g

我走这条路的原因是因为我想确保我的文件是非破坏性的。 我可以使用 :%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.

/^M

should select the first bad line

:%s/^M/\r/g

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:

:%s/\r$/ /g
:%s/ $//g

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.

阳光下的泡沫是彩色的 2024-07-26 03:42:28

这些都不适合我,所以我尝试了这个,它有效:

输入 :%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).

迎风吟唱 2024-07-26 03:42:28

^M 由 Ctrl+VM 检索,因此也是如此

s/^M//g

^M is retrieved by Ctrl+V and M, so do

s/^M//g
最舍不得你 2024-07-26 03:42:28

无需使用 Ctrl:
<代码>:%s/\r$//

Without needing to use Ctrl:
:%s/\r$//

巴黎盛开的樱花 2024-07-26 03:42:28

对我有用的简单事情

dos2unix   filename

Simple thing that worked for me

dos2unix   filename
守护在此方 2024-07-26 03:42:28

我用 sed 做到了这一点:

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

I did this with sed:

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

提笔书几行 2024-07-26 03:42:28

怎么样:
<代码>
:%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.

心房的律动 2024-07-26 03:42:28

这个问题还有很多其他答案,但以下内容仍然最适合我,因为我需要命令行解决方案:

vim -u NONE -c 'e ++ff=dos' -c 'w ++ff=unix' -c q myfile

说明:

  • 在不加载任何 .vimrc 文件的情况下,打开 myfile
  • 运行 : e ++ff=dos 强制重新加载整个文件作为 dos 行结尾。
  • 运行 :w ++ff=unix 以使用 unix 行结尾写入文件
  • 退出 vim

There are many other answers to this question, but still, the following works best for me, as I needed a command line solution:

vim -u NONE -c 'e ++ff=dos' -c 'w ++ff=unix' -c q myfile

Explanation:

  • Without loading any .vimrc files, open myfile
  • Run :e ++ff=dos to force a reload of the entire file as dos line endings.
  • Run :w ++ff=unix to write the file using unix line endings
  • Quit vim
月隐月明月朦胧 2024-07-26 03:42:28

Ctrl+M 最小化了我的窗口,但 Ctrl+Enter 实际上插入了一个 ^M 性格。 我还必须确保在两次按下之间不要松开 Ctrl 键。

所以我的解决方案是:

:%s/<Ctrl-V><Ctrl-Enter>/\r/g

其中 表示按住 Ctrl,然后按松开V,按下并松开Enter然后松开Ctrl

如果您正在处理 Windows 生成的文件

上述解决方案将在现有行之间添加额外的行,因为 ^M 后面已经有一个不可见的 \r

为了防止这种情况,您需要删除 ^M 字符而不替换它们。

:%s/<Ctrl-V><Ctrl-Enter>//g

其中 % 表示“在此缓冲区中”,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:

:%s/<Ctrl-V><Ctrl-Enter>/\r/g

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.

:%s/<Ctrl-V><Ctrl-Enter>//g

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"), and g is a flag meaning "globally," as opposed to the first occurrence in a line.

人生百味 2024-07-26 03:42:28

这对我有用:

  1. 将文件格式设置为unix(\n行结尾)
  2. 保存文件

所以在vim中:

:set ff=unix
:w

This worked for me:

  1. Set file format to unix (\n line ending)
  2. save the file

So in vim:

:set ff=unix
:w
桃酥萝莉 2024-07-26 03:42:28

就我而言,

以上都不起作用,我从我的Mac复制了一个CSV文件到Linux机器,我使用了上述所有命令,但没有任何帮助,但下面的命令

tr "\015" "\n" < inputfile > outputfile

我有一个文件,其中^M字符夹在如下所示的行之间

Audi,A4,35 TFSi Premium,,CAAUA4TP^MB01BNKT6TG,TRO_WBFB_500,Trico,CARS,Audi,A4,35 TFSi Premium,,CAAUA4TP^MB01BNKTG0A,TRO_WB_T500,Trico,

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

tr "\015" "\n" < inputfile > outputfile

I had a file in which ^M characters were sandwitched between lines something like below

Audi,A4,35 TFSi Premium,,CAAUA4TP^MB01BNKT6TG,TRO_WBFB_500,Trico,CARS,Audi,A4,35 TFSi Premium,,CAAUA4TP^MB01BNKTG0A,TRO_WB_T500,Trico,
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文