如果我打开在Windows中创建的文件,所有行都以^M结尾。如何一次删除这些字符?


当前回答

:set fileformat=unix将DOS转换为unix。

其他回答

更改视图中的行尾:

:e ++ff=dos
:e ++ff=mac
:e ++ff=unix

这也可以用作保存操作(:w单独保存不会使用屏幕上看到的行尾):

:w ++ff=dos
:w ++ff=mac
:w ++ff=unix

您可以从命令行使用它:

for file in *.cpp
do 
    vi +':w ++ff=unix' +':q' "$file"
done

在VIM中:

:e ++ff=dos | set ff=unix | w!

带VIM的外壳:

vim some_file.txt +'e ++ff=dos | set ff=unix | wq!'

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

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

以下步骤可以将DOS的文件格式转换为Unix:

:e ++ff=dos     Edit file again, using dos file format ('fileformats' is ignored).[A 1]
:setlocal ff=unix     This buffer will use LF-only line endings when written.[A 2]
:w     Write buffer using Unix (LF-only) line endings.

参考:文件格式

可以使用以下命令::%s/^V^M//g其中“^”表示使用CTRL键。

dos2unix可以直接修改文件内容。

您可以直接在文件上使用它,而不需要临时文件重定向。

dos2unix input.txt input.txt

以上使用假定的美国键盘。使用-437选项使用英国键盘。

dos2unix -437 input.txt input.txt