为什么 Vim 在连接行时要添加空格?
我想在 Vim 中解开文本。当我连接行时,句子之间会出现额外的空格。
这是为什么?
I want to unwrap text in Vim. When I join lines I get an additional space between sentences.
Why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我有一种感觉,这就是您真正想要的: gJ
From
:h gJ
:如果您从终端复制了某些内容并将其粘贴为大文件,这会很方便vim 中的矩形块,而不是单行。
我通常在视觉模式下使用它。突出显示内容,gJ。
I have a feeling this is what you really want: gJ
From
:h gJ
:This is handy if you've copied something from a terminal and it's pasted it as a big rectangular block into vim, rather than a single line.
I usually use it in visual mode. Hilight stuff, gJ.
格式化会破坏信息。有许多不同的文本块,一旦格式化,就会产生相同的文本块。因此,在没有先验知识的情况下无法逆转操作(即撤消)。
未格式化: 已
格式化:
如果您希望段落全部在一行上,或者如果您可以进行一些手动调整,则可以使用
J
将各行重新连接在一起。您可以使用可视模式将J
命令一次应用于多行,也可以与ap
或ip
结合使用来选择段落,例如 <代码>vipJ。同样,您仍然会丢失一些信息 - 格式化之前换行符处的多个空格最终会折叠为单个空格。 (实际上,您可以通过使用gJ
而不是J
来加入而不修改空格,但在格式化时您已经丢失了它们)如果您被多余的空格困扰在句子之后(以 !、? 或 . 结尾的行),关闭连接空间:
set nojoinspaces
Formatting destroys information. There are many different blocks of text which will result in the same one once formatted. Therefore, there's no way to reverse the operation without prior knowledge (i.e. undo).
Unformatted:
Formatted:
If you want your paragraph all on one line, or if you're okay with a little manual fiddling, you can use
J
to join lines back together. You can use visual mode to apply theJ
command to several lines at once, perhaps combined withap
orip
to select a paragraph, e.g.vipJ
. Again, you'll still lose some information - multiple spaces at line breaks before formatting will end up collapsed to single spaces. (You can actually join without modifying spaces by usinggJ
instead ofJ
, but you'll already have lost them when you formatted)If you're bothered by the extra spaces after sentences (lines ending in !, ?, or .), turn off joinspaces:
set nojoinspaces
我想连接行之间没有空格的简单解决方案是:
使用
!
连接不会插入或删除任何空格。对于整个文件,请使用:%j!
。请参阅:
:help :join
。I guess the simple solution to join the lines without spaces between is:
With
!
the join does not insert or delete any spaces. For the whole file, use:%j!
.See:
:help :join
.这是最终对我有用的答案,以上都不适用于我的用例。
本质上,像其他人所说的那样使用 gJ,但突出显示所有文件,因此在命令模式下键入 ggVGgJ。
This is the answer that ended up working for me, none of the above worked in my use case.
Essentially, use gJ like multiple others have said, but highlight all of file, so in command mode typing ggVGgJ.
如果我们处理的行不以空格结尾,我在连接后仍然会得到额外的一个空格。通常这是所需的行为。 示例
使用
J
连接后的,变为虽然在某些情况下,我们不希望应用它,
但我们希望连接变为
myInstance->methodA()->methodB( )
之间没有任何空格!助手映射;
这里我使用的 可以使用
:let mapleader
检查密钥,我相信默认为密钥\
。所以在正常模式下,只需
\jj
即可执行连接,无需任何额外空间!I still got the extra one space after join, if the line we work on does not end with space. Usually this is the desired behaviour. Example
after joining with
J
, becomeAlthough in some case, we do not wish to apply it,
And we would want the join to become
myInstance->methodA()->methodB()
without any space in between!Here the helpers mapping i use
<leader> key can be checked with
:let mapleader
, default to key\
i believe.so in normal mode, just
\jj
to perform join without any extra space!