[英]In Vim, what is the simplest way to join all lines in a file into a single line?
I want to join all lines in a file into a single line. 我想将文件中的所有行连接成一行。 What is the simplest way of doing this?
这样做最简单的方法是什么? I've had poor luck trying to use substitution (
\\r\\n
or \\n
doesn't seem to get picked up correctly in the case of s/\\r\\n//
on Windows). 我试图使用替换运气不好(
\\r\\n
或\\n
在Windows上s/\\r\\n//
的情况下似乎没有正确选取)。 Using J
in a range expression doesn't seem to work either (probably because the range is no longer in 'sync' after the first command is executed). 在范围表达式中使用
J
似乎也不起作用(可能是因为在执行第一个命令后范围不再处于'sync')。
I tried :1,$norm! J
我试过了
:1,$norm! J
:1,$norm! J
but this only did half of the file - which makes sense because it just joins each line once. :1,$norm! J
但这只占了文件的一半 - 这是有道理的,因为它只加入每一行一次。
Another way: 其他方式:
ggVGJ
" ggVG
" visually selects all lines, and " J
" joins them. “
ggVG
”在视觉上选择所有行,“ J
”加入它们。
Ah, I found the answer. 啊,我找到了答案。
:1,$join
Works like a charm. 奇迹般有效。
EDIT : As pointed out in the comment: 编辑 :正如评论中指出:
:%join -or- :%j
...removes the range. ...删除范围。
You can do it with 3 keystrokes starting from normal mode: 您可以从正常模式开始执行3次击键:
:%j
:
enters command mode :
进入命令模式 %
refers to all lines in the file %
表示文件中的所有行 j
executes the join command j
执行join命令 Now it seems that this adds a space between the lines. 现在似乎这在线之间增加了一个空格。 I am not sure if you want this.
我不确定你是否想要这个。
You can do it in three fewer keystrokes: 你可以减少三次击键:
:1,$j
isn't ed grand? 是不是盛大?
Cryptic way: 神秘的方式:
qqqqqJ@qq@q
(the first three q
's clear the q
register, the qqJ@qq
records a macro to the q
register that performs a Join, then calls q
, and the last @q
runs it. (前三个
q
清除q
寄存器, qqJ@qq
将一个宏记录到执行Join的q
寄存器,然后调用q
,最后一个@q
运行它。
I'm surprised no one even mentioned the other way: 我很惊讶没有人甚至提到过另一种方式:
:%s/\n/ /
I am equally surprised that no one pointed out that the range 1,$
has a shorthand that's written %
. 我同样感到惊讶的是,没有人指出范围
1,$
有一个写的%
的速记。
(This doesn't do the same thing as joining the lines, but depending on circumstances that may in fact be more appropriate.) (这与加入线条的方式不同,但取决于实际上可能更合适的情况。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.