为什么 Vim 在连接行时要添加空格?

发布于 2024-08-07 19:26:38 字数 56 浏览 5 评论 0原文

我想在 Vim 中解开文本。当我连接行时,句子之间会出现额外的空格。

这是为什么?

I want to unwrap text in Vim. When I join lines I get an additional space between sentences.

Why is that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

维持三分热 2024-08-14 19:26:38

我有一种感觉,这就是您真正想要的: gJ

From :h gJ:

gJ          Join [count] lines, with a minimum of two lines.
            Don't insert or remove any spaces.  {not in Vi}

如果您从终端复制了某些内容并将其粘贴为大文件,这会很方便vim 中的矩形块,而不是单行。

我通常在视觉模式下使用它。突出显示内容,gJ

I have a feeling this is what you really want: gJ

From :h gJ:

gJ          Join [count] lines, with a minimum of two lines.
            Don't insert or remove any spaces.  {not in Vi}

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.

谁许谁一生繁华 2024-08-14 19:26:38

格式化会破坏信息。有许多不同的文本块,一旦格式化,就会产生相同的文本块。因此,在没有先验知识的情况下无法逆转操作(即撤消)。

未格式化: 已

Unformatted text could start out as either all one line, or several, yet look the same when formatted.

Unformatted text could start out as either all one line, or several, 
yet look the same when formatted.

格式化:

Unformatted text could start out as 
either all one line, or several, 
yet look the same when formatted.

如果您希望段落全部在一行上,或者如果您可以进行一些手动调整,则可以使用 J 将各行重新连接在一起。您可以使用可视模式将 J 命令一次应用于多行,也可以与 apip 结合使用来选择段落,例如 <代码>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:

Unformatted text could start out as either all one line, or several, yet look the same when formatted.

Unformatted text could start out as either all one line, or several, 
yet look the same when formatted.

Formatted:

Unformatted text could start out as 
either all one line, or several, 
yet look the same when 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 the J command to several lines at once, perhaps combined with ap or ip 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 using gJ instead of J, 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

满身野味 2024-08-14 19:26:38

我想连接行之间没有空格的简单解决方案是:

:j!

使用 ! 连接不会插入或删除任何空格。对于整个文件,请使用 :%j!

请参阅::help :join

I guess the simple solution to join the lines without spaces between is:

:j!

With ! the join does not insert or delete any spaces. For the whole file, use :%j!.

See: :help :join.

仙气飘飘 2024-08-14 19:26:38

是最终对我有用的答案,以上都不适用于我的用例。

本质上,像其他人所说的那样使用 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.

葵雨 2024-08-14 19:26:38

如果我们处理的行不以空格结尾,我在连接后仍然会得到额外的一个空格。通常这是所需的行为。 示例

first line without space
second line

使用 J 连接后的

first line without space second line

,变为虽然在某些情况下,我们不希望应用它,

myInstance->methodA()
          ->methodB()

但我们希望连接变为 myInstance->methodA()->methodB( ) 之间没有任何空格!

助手映射;

nmap <leader>jj Jx

这里我使用的 可以使用 :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

first line without space
second line

after joining with J, become

first line without space second line

Although in some case, we do not wish to apply it,

myInstance->methodA()
          ->methodB()

And we would want the join to become myInstance->methodA()->methodB() without any space in between!

Here the helpers mapping i use

nmap <leader>jj Jx

<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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文