如何在行尾删除^ M?

时间:2014-01-11 13:36:28

标签: vi

我在xp + gvim工作。

                    mpg cyl  disp  hp drat    wt  qsec vs am gear carb^M
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2^M
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4^M
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4^M
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4^M
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1^M
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2^M
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4

我通过命令获取数据

:r  !sort  /cygdrive/c/workspace/accountant.txt

我想删除 ^ M

1.method1

:%s/\^M$//g  
pattern not found ^M   

0d0a是^ M,为什么:%s/\x0d0a//g无法运行?

2.method2

dos2unix  /cygdrive/c/workspace/accountant.txt
dos2unix: converting file /cygdrive/c/workspace/accountant.txt to Unix format.

打开目标文件时没有任何改变。

3.mehtod3

:!s/.\{2}$//g

方法3没问题。

2 个答案:

答案 0 :(得分:1)

我的.vimrc

中有这个代码段
command! KillControlM :normal :%s/<C-V><C-M>//e<CR><C-O><CR>

只需在命令模式下键入:KillControlM即可使用它。)

答案 1 :(得分:1)

我总是对所有行使用:%s/\r$// - “,在行尾没有任何东西替换回车符。”