简体   繁体   English

在Vim中,将文件中的所有行连接成一行的最简单方法是什么?

[英]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.

相关问题 如何在 Windows 中将单个文本文件中的多行合并为一行? - How to combine multiple lines in a single text file into one line, in Windows? 区分Windows版本的最简单方法是什么? - What is the simplest way to differentiate between Windows versions? 以二进制模式写入标准输出的最简单方法是什么? - What is the simplest way to write to stdout in binary mode? 记录文件读取操作的最简单方法 - Simplest way to log file read operations Windows 批处理文件插入空行/删除单行 - nmap 输出 - Windows batch file to insert a blank line / remove single lines - nmap output 将多行批处理成一行? - Batch processing multiple lines to a single line? 使用PowerShell打开.txt文件中的URL最简单的方法 - Simplest way to open the URL in the .txt file using PowerShell 检测文件中使用哪些换行符的可靠方法是什么? - What is a reliable way to detect which line break characters are used in a file? 为什么只执行此 Windows 批处理文件的第一行,而在命令 shell 中执行所有三行? - Why does only the first line of this Windows batch file execute but all three lines execute in a command shell? 在命令行中从较大的数据集中删除较小的数据集的最简单方法 - Simplest way to remove smaller data set from bigger one in command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM