英文:
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 <C-v>2j$A
实际上不是达到你最初目标的高效方法。使用一个简单的替换命令如:
:,+2s/.*/&
会是一个更好的方法。
英文:
The command you are asking about:
<C-v>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:
<C-v>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:
<C-v>2jI`
for the first step, and:
<C-v>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:
<C-v>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.
Note also that:
<C-v>2jI`
<C-v>2j$A`
is not really an efficient method for reaching your initial goal. Using a simple substitution like:
:,+2s/.*/`&`
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 '<,'>
will be automatically added.
See :help :normal-range
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论