在Vim中编辑多行以在文本末尾添加文本。

huangapple go评论57阅读模式
英文:

vim editing multiple lines to add text at the end of text

问题

I have a following block of text.

`[1,2,3,4]`
`[1,2,4]`
`[1,2]`

I want to add a backtick ( ` ) at the end of each line. However when I try the following key combination <C-v>4jI, it does add the backtick at the end of line but there is some space introduced. How do I add it at the end of the text?

With the command C-v4jI, the output is like this:

`[1,2,3,4]`
`[1,2,4]  `
`[1,2]    `

What I want is like this:

`[1,2,3,4]`
`[1,2,4]`
`[1,2]`
英文:

I have a following block of text.

`[1,2,3,4]
`[1,2,4]
`[1,2]

I want to add a backtick ( ` ) at the end of each line. However when I try the following key combination <C-v>4jI, it does add the backtick at the end of line but there is some space introduced. How do I add it at the end of the text?

With the command C-v4jI, the output is like this:

`[1,2,3,4]`
`[1,2,4]  `
`[1,2]    `

What I want is like this:

`[1,2,3,4]`
`[1,2,4]`
`[1,2]`

答案1

得分: 3

选择这些行(例如,V4j),并使用:

s/$/`/

如果已经存在尾随空白,您可能需要:

s/ *$/`/

或类似的操作。

英文:

Select the lines (eg, V4j) and use:

s/$/`/

If there is trailing whitespace already present, you may need:

s/ *$/`/

or similar

答案2

得分: 0

以下是翻译好的内容:

你所询问的命令:

<C-v>4jI`

实际上是用来获得这个问题的初始状态的命令:

[1,2,3,4] [1,2,4]
`[1,2]

要从前一个状态获取这个状态:

[1,2,3,4]
[1,2,4]
[1,2]

你需要在第一行的最后一个字符上执行以下操作:

<C-v>4jA`

这实际上是你应该在问题中提到的命令。

请注意,这里只有三行,所以实际上应该是:

<C-v>2jI`

作为第一步,和:

<C-v>2jA`

作为第二步。

现在你应该能够使用以下命令获得所需的结果,在第一行的第一个字符上执行

<C-v>2j$A`

注意$,它扩展了每行的可视区域的末尾。这样可以在每行的末尾插入内容。

请注意:

<C-v>2jI &lt;C-v&gt;2j$A

实际上不是达到你最初目标的高效方法。使用一个简单的替换命令如:

:,+2s/.*/&

会是一个更好的方法。

英文:

The command you are asking about:

&lt;C-v&gt;4jI`

is actually the one you used to obtain the initial state of this question:

`[1,2,3,4]
`[1,2,4]
`[1,2]

To obtain this from the previous state:

`[1,2,3,4]`
`[1,2,4]  `
`[1,2]    `

you did the following with the cursor on the last character of the first line:

&lt;C-v&gt;4jA`

which is the actual command you should have mentioned in your question.

Note that you only have three lines, here, so it should have actually been:

&lt;C-v&gt;2jI`

for the first step, and:

&lt;C-v&gt;2jA`

for the second step.

Now that that is out of the way, you should be able to obtain the desired result with the following command, executed with the cursor on the first character of the first line:

&lt;C-v&gt;2j$A`

Note the $, which extends the visual area to the end of each concerned line. That is what allows you to insert something at the end of each line.

在Vim中编辑多行以在文本末尾添加文本。

Note also that:

&lt;C-v&gt;2jI`
&lt;C-v&gt;2j$A`

is not really an efficient method for reaching your initial goal. Using a simple substitution like:

:,+2s/.*/`&amp;`

would have been a much better approach.

答案3

得分: 0

After selecting the lines you can use:

:normal A`

Note that the range '<,'> will be automatically added.
See :help :normal-range

英文:

After selecting the lines you can use:

:normal A`

Note that the range &#39;&lt;,&#39;&gt; will be automatically added.
See :help :normal-range

huangapple
  • 本文由 发表于 2023年4月13日 23:12:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定