windows 在 Vim 中,将文件中的所有行合并为一行的最简单方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/391710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Vim, what is the simplest way to join all lines in a file into a single line?
提问by Jordan Parmer
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). 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).
我想将文件中的所有行合并为一行。这样做的最简单方法是什么?我在尝试使用替换时运气不佳(\r\n
或者在 Windows\n
的情况下似乎没有正确选择s/\r\n//
)。J
在范围表达式中使用似乎也不起作用(可能是因为在执行第一个命令后范围不再处于“同步”状态)。
I tried :1,$norm! J
but this only did half of the file - which makes sense because it just joins each line once.
我试过,:1,$norm! J
但这只完成了文件的一半 - 这是有道理的,因为它只连接每一行一次。
回答by orip
Another way:
其它的办法:
ggVGJ
"ggVG
" visually selects all lines, and "J
" joins them.
" ggVG
" 直观地选择所有行,然后 " J
" 将它们连接起来。
回答by Jordan Parmer
Ah, I found the answer.
啊,我找到了答案。
:1,$join
Works like a charm.
奇迹般有效。
EDIT: As pointed out in the comment:
编辑:正如评论中指出的那样:
:%join -or- :%j
...removes the range.
...删除范围。
回答by Dimi
You can do it with 3 keystrokes starting from normal mode:
从正常模式开始,您可以通过 3 次击键来完成:
:%j
:
enters command mode%
refers to all lines in the filej
executes the join command
:
进入命令模式%
指文件中的所有行j
执行连接命令
Now it seems that this adds a space between the lines. I am not sure if you want this.
现在看来,这在两行之间增加了一个空格。我不确定你是否想要这个。
回答by tpgould
You can do it in three fewer keystrokes:
您只需按三下键即可完成:
:1,$j
isn't ed grand?
不是 ed 大吗?
回答by Josh Lee
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
宏记录到q
执行Join的寄存器中,然后调用q
,最后一个@q
运行它。
回答by Aristotle Pagaltzis
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.)
(这与加入线路的作用不同,但取决于实际上可能更合适的情况。)