[英]Convert DOS/Windows line endings to Linux line endings in Vim
If I open files I created in Windows, the lines all end with ^M
.如果我打开我在 Windows 中创建的文件,所有行都以^M
结尾。
How do I delete these characters all at once?如何一次删除所有这些字符?
dos2unix is a commandline utility that will do this, or :%s/^M//g
will if you use Ctrl - v Ctrl - m to input the ^M, or you can :set ff=unix
and Vim will do it for you. dos2unix是一个可以执行此操作的命令行实用程序,或者:%s/^M//g
如果您使用Ctrl - v Ctrl - m来输入 ^M,或者您可以:set ff=unix
并且 Vim 将执行此操作你。
There is documentation on the fileformat setting, and the Vim wiki has a comprehensive page on line ending conversions .有关于文件格式设置的文档,Vim wiki 有一个关于行结束转换的综合页面。
Alternately, if you move files back and forth a lot, you might not want to convert them, but rather to do :set ff=dos
, so Vim will know it's a DOS file and use DOS conventions for line endings.或者,如果您来回移动文件很多,您可能不想转换它们,而是执行:set ff=dos
,因此 Vim 将知道它是一个 DOS 文件并使用 DOS 约定作为行尾。
Change the line endings in the view:更改视图中的行尾:
:e ++ff=dos
:e ++ff=mac
:e ++ff=unix
This can also be used as saving operation (:w alone will not save using the line endings you see on screen):这也可以用作保存操作(单独使用 :w 不会使用您在屏幕上看到的行结尾进行保存):
:w ++ff=dos
:w ++ff=mac
:w ++ff=unix
And you can use it from the command-line:您可以从命令行使用它:
for file in *.cpp
do
vi +':w ++ff=unix' +':q' "$file"
done
I typically use我通常使用
:%s/\r/\r/g
which seems a little odd, but works because of the way that Vim matches linefeeds.这看起来有点奇怪,但由于 Vim 匹配换行符的方式而起作用。 I also find it easier to remember :)我也发现它更容易记住:)
I prefer to use the following command:我更喜欢使用以下命令:
:set fileformat=unix
You can also use mac
or dos
to respectively convert your file to Mac or MS-DOS/Windows file convention.您也可以使用mac
或dos
分别将您的文件转换为 Mac 或 MS-DOS/Windows 文件约定。 And it does nothing if the file is already in the correct format.如果文件已经是正确的格式,它什么也不做。
For more information, see the Vim help:有关更多信息,请参阅 Vim 帮助:
:help fileformat
:set fileformat=unix
从 DOS 转换到 Unix。
:%s/\r\+//g
在 Vim 中,这会去除所有回车符,只留下换行符。
From: File format来自: 文件格式
[Esc] :%s/\\r$// [Esc] :%s/\\r$//
@ https://gist.github.com/sparkida/7773170 @ https://gist.github.com/sparkida/7773170
find $(pwd) -type f -name "*.ext" | while read file; do sed -e 's/^M//g' -i "$file"; done;
Also, as mentioned in a previous answer, ^M = Ctrl+V + Ctrl+M (don't just type the caret "^" symbol and M).此外,如上一个答案中所述, ^M = Ctrl+V + Ctrl+M (不要只输入插入符号“^”和 M)。
dos2unix
can directly modify the file contents. dos2unix
可以直接修改文件内容。
You can directly use it on the file, without any need for temporary file redirection.您可以直接在文件上使用它,无需临时文件重定向。
dos2unix input.txt input.txt
The above uses the assumed US keyboard.以上使用假定的美国键盘。 Use the -437
option to use the UK keyboard.使用-437
选项来使用英国键盘。
dos2unix -437 input.txt input.txt
tr -d '\15\32' < winfile.txt > unixfile.txt
The following steps can convert the file format for DOS to 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.
Reference: File format参考: 文件格式
The comment about getting the ^M to appear is what worked for me.关于让 ^M 出现的评论对我有用。 Merely typing "^M" in my vi got nothing (not found).仅仅在我的 vi 中输入“^M”什么也没有(找不到)。 The CTRL + V CTRL + M sequence did it perfectly though.不过CTRL + V CTRL + M序列做得很完美。
My working substitution command was我的工作替换命令是
:%s/Ctrl-V Ctrl-M/\r/g
and it looked like this on my screen:它在我的屏幕上看起来像这样:
:%s/^M/\r/g
In VIM:在 VIM 中:
:e ++ff=dos | set ff=unix | w!
In shell with VIM:在带有 VIM 的 shell 中:
vim some_file.txt +'e ++ff=dos | set ff=unix | wq!'
e ++ff=dos
- force open file in dos
format. e ++ff=dos
- 强制以dos
格式打开文件。
set ff=unix
- convert file to unix
format. set ff=unix
- 将文件转换为unix
格式。
With the following command:使用以下命令:
:%s/^M$//g
To get the ^M
to appear, type Ctrl V and then Ctrl M .要使^M
出现,请输入Ctrl V ,然后输入Ctrl M 。 Ctrl V tells Vim to take the next character entered literally. Ctrl V告诉 Vim 按字面意思输入下一个字符。
I found a very easy way: Open the file with nano : nano file.txt
我找到了一个非常简单的方法:用nano打开文件: nano file.txt
Press Ctrl + O to save, but before pressing Enter , press: Alt + D to toggle between DOS and Unix/Linux line-endings, or: Alt + M to toggle between Mac and Unix/Linux line-endings, and then press Enter to save and Ctrl + X to quit.按Ctrl + O保存,但在按Enter之前,按: Alt + D在 DOS 和 Unix/Linux 行尾之间切换,或: Alt + M在 Mac 和 Unix/Linux 行尾之间切换,然后按Enter保存和Ctrl + X退出。
要直接在 Linux 控制台中运行:
vim file.txt +"set ff=unix" +wq
:g/Ctrl-v Ctrl-m/s///
Ctrl M is the character \\r
, or carriage return, which DOS line endings add. Ctrl M是字符\\r
或回车,DOS 行尾添加。 Ctrl V tells Vim to insert a literal Ctrl M character at the command line. Ctrl V告诉 Vim 在命令行插入一个文字Ctrl M字符。
Taken as a whole, this command replaces all \\r
with nothing, removing them from the ends of lines.总的来说,这个命令将所有\\r
替换为空,从行尾删除它们。
You can use the following command:您可以使用以下命令:
:%s/^V^M//g
where the '^' means use CTRL key.其中“^”表示使用CTRL键。
Usually there is a dos2unix
command you can use for this.通常有一个dos2unix
命令可以用于此目的。 Just make sure you read the manual as the GNU and BSD versions differ on how they deal with the arguments.只要确保您阅读了手册,因为 GNU 和 BSD 版本在处理参数的方式上有所不同。
BSD version: BSD 版本:
dos2unix $FILENAME $FILENAME_OUT
mv $FILENAME_OUT $FILENAME
GNU version: GNU版本:
dos2unix $FILENAME
Alternatively, you can create your own dos2unix
with any of the proposed answers here, for example:或者,您可以使用此处提出的任何答案创建自己的dos2unix
,例如:
function dos2unix(){
[ "${!}" ] && [ -f "{$1}" ] || return 1;
{ echo ':set ff=unix';
echo ':wq';
} | vim "${1}";
}
From Wikia:来自维基:
%s/\r\+$//g
That will find all carriage return signs (one and more reps) up to the end of line and delete, so just \\n
will stay at EOL.这将找到直到行尾的所有回车符(一个或多个代表)并删除,因此\\n
将留在 EOL。
This is my way.这是我的方式。 I opened a file in DOS EOL and when I save the file, that will automatically convert to Unix EOL:我在 DOS EOL 中打开了一个文件,当我保存该文件时,它将自动转换为 Unix EOL:
autocmd BufWrite * :set ff=unix
I wanted newlines in place of the ^M's.我想要换行符代替 ^M 的。 Perl to the rescue: Perl 来拯救:
perl -pi.bak -e 's/\x0d/\n/g' excel_created.txt
Or to write to stdout:或者写入标准输出:
perl -p -e 's/\x0d/\n/g' < excel_created.txt
If you create a file in Notepad or Notepad++ in Windows, bring it to Linux, and open it by Vim, you will see ^M at the end of each line.如果你在 Windows 中用 Notepad 或Notepad++创建一个文件,把它带到 Linux,然后用 Vim 打开它,你会在每一行的末尾看到 ^M。 To remove this,要删除这个,
At your Linux terminal, type在您的 Linux 终端,输入
dos2unix filename.ext
This will do the required magic.这将完成所需的魔法。
This is a little more than you asked for but:这比您要求的要多一点,但是:
nmap <C-d> :call range(line('w0'),line('w$'))->map({_,v-> getline(v)})->map({_,v->trim(v,join(map(range(1,0x1F)+[0xa0],{n->n->nr2char()}),''),2)})->map({k,v->setline(k+1,v)})<CR>
Run this and :set ff=unix|dos
and no more need for unix2dos.运行它并:set ff=unix|dos
并且不再需要 unix2dos。
:set fileformat=
does at this point is choose which eol to save it with, dos or unix所有:set fileformat=
此时做的是选择用哪个 eol 保存它,dos 或 unix
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.