如何在vi中从光标位置开始插入一块空格?
假设我有下面一段文字,当前光标停留在第一个 A
上,
AAAA
BBB
CC
D
如何在每行前面添加空格以使其像这样,如果列数就很好了空间的数量可以即时指定,例如,这里是两个。
AAAA
BBB
CC
D
我想有一种方法可以在视觉模式下快速完成它,但是有什么想法吗?
目前我正在视觉模式下将第一列文本复制两次,并将整个两列替换为空格,其中涉及 > 5个按键,太麻烦了。
约束:
抱歉,我没有清楚地陈述问题,可能会造成一些混乱。
目标只是较大文件的一部分,因此如果可以指定从第一个 A
开始的行数和列数,那就太好了。
编辑:
感谢@DeepYellow和@Johnsyweb,显然>}
和>ap
都是我不知道的很棒的提示,在我澄清问题答案的具体要求之前,它们都可能是有效的答案,但无论如何,@luser droog 的答案都是唯一可行的答案。谢谢大家!
Suppose I have the piece of text below with the cursor staying at the first A
currently,
AAAA
BBB
CC
D
How can I add spaces in front of each line to make it like, and it would be great if the number of columns of spaces can be specified on-the-fly, e.g., two here.
AAAA
BBB
CC
D
I would imagine there is a way to do it quickly in visual mode, but any ideas?
Currently I'm copying the first column of text in visual mode twice, and replace the entire two column to spaces, which involves > 5 keystrokes, too cumbersome.
Constraint:
Sorry that I didn't state the question clearly and might create some confusions.
The target is only part of a larger file, so it would be great if the number of rows and columns starting from the first A
can be specified.
Edit:
Thank both @DeepYellow and @Johnsyweb, apparently >}
and >ap
are all great tips that I was not aware of, and they both could be valid answers before I clarified on the specific requirement for the answer to my question, but in any case, @luser droog 's answer stands out as the only viable answer. Thank you everyone!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我会使用
:%s/^/ /
您还可以指定行范围
:10,15s/^/ /
或相对范围
:。 ,+5s/^/ /
或者对位置使用正则表达式
:/A/,/D/>
。为了复制代码以粘贴到 SO 上,我通常从终端使用 sed
sed 's/^/ /' filename
快捷方式
我刚刚学到了一个新技巧。您进入视觉模式
v
,选择区域(使用常规移动命令),然后点击:
,这将为您提供:准备好输入只是上述命令的 command 部分,标记
'<
和'>
自动设置为视觉选择的边界。选择并缩进当前段落:
或
按回车。
编辑:
根据评论中的要求,您还可以在 any 上使用正则表达式量词
\{n}
在行的中间添加空格> 元字符.
。这会在每行左侧添加一个 14 个字符的空格。当然,
%
可以替换为上述任何选项,用于指定 ex 命令的范围。I'd use
:%s/^/ /
You could also specify a range of lines
:10,15s/^/ /
Or a relative range
:.,+5s/^/ /
Or use regular expressions for the locations
:/A/,/D/>
.For copying code to paste on SO, I usually use sed from the terminal
sed 's/^/ /' filename
Shortcut
I just learned a new trick for this. You enter visual mode
v
, select the region (with regular movement commands), then hit:
which gives you this:ready for you to type just the command part of the above commands, the marks
'<
and'>
being automatically set to the bounds of the visual selection.To select and indent the current paragraph:
or
followed by enter.
Edit:
As requested in the comments, you can also add spaces to the middle of a line using a regex quantifier
\{n}
on the any meta-character.
.This adds a space 14 chars from the left on each line. Of course
%
could be replaced by any of the above options for specifying the range of an ex command.当在第一个 A 上时,我会进入块视觉模式
ctrl-v
,选择要修改的行,按I
(使用大写插入模式i
),并对第一行应用我想要的任何更改。离开可视模式esc
会将第一行上的所有更改应用到所有行。可能不是最有效的击键次数,但在离开视觉模式之前为您提供了所需的所有自由。当我必须在正则表达式命令中手动指定行和列范围时,我不喜欢它。
When on the first A, I'd go in block visual mode
ctrl-v
, select the lines you want to modify, pressI
(insert mode with capitali
), and apply any changes I want for the first line. Leaving visual modeesc
will apply all changes on the first line to all lines.Probably not the most efficient on number of key-strokes, but gives you all the freedom you want before leaving visual mode. I don't like it when I have to specify by hand the line and column range in a regex command.
我会使用 >}。
其中...
>
:右移并}
:表示直到段落结束希望这会有所帮助。
I'd use >}.
Where...
>
: Shifts right and}
: means until the end of the paragraphHope this helps.
我会喜欢尼古。另一种解决方案是使用
:normal
:
进入 VISUAL-LINE 模式3j
或jjj
或/D
选择行:norm I
,正确的范围('<,'>
) 自动插入:normal
对于这种特定情况可能有点矫枉过正,但有时您可能想要执行一堆复杂的操作一系列线路。I would do like Nigu. Another solution is to use
:normal
:<S-v>
to enter VISUAL-LINE mode3j
orjjj
or/D<CR>
to select the lines:norm I<Space><Space>
, the correct range ('<,'>
) being inserted automatically:normal
is probably a bit overkill for this specific case but sometimes you may want to perform a bunch of complex operations on a range of lines.您可以在可视模式下选择行,然后键入
>
。这假设您已将制表符设置为插入空格,例如:(将 4 替换为您在缩进中的首选项)
如果这些行形成一个段落,则正常模式下的
>ap
将移动整个段落当前位置的上方和下方。You can select the lines in visual mode, and type
>
. This assumes that you've set your tabs up to insert spaces, e.g.:(replace 4 with your preference in indentation)
If the lines form a paragraph,
>ap
in normal mode will shift the whole paragraph above and below the current position.假设您要移动一段代码:
:set shiftwidth=1
,默认为 8。Ctrl+v
在适当的位置并将光标向上k
或向下j
移动以选择某个区域。>
移动方块,按.
重复该操作,直到所需位置(如果光标丢失,请按h
或返回>b
)。Let's assume you want to shift a block of code:
:set shiftwidth=1
, default is 8.Ctrl+v
in appropriate place and move cursor upk
or downj
to select some area.>
to shift the block and.
to repeat the action until desired position (if cursor is missed, turn back withh
orb
).您可以尝试的另一件事是宏。如果您还不知道,请使用
q
启动宏,然后选择寄存器来保存宏...因此,要将宏保存在寄存器a
中,您可以输入 < code>qa 在正常模式下。底部应该有一些写着录音的内容。现在就按照你想要的方式做你的动作。
因此,在这种情况下,您需要在每行前面有 2 个空格,因此当光标已经位于第一行的开头时,进入插入模式,然后按两次空格。现在按 escape 进入正常模式,然后向下到下一行,然后到该行的开头,然后按
q
。这将结束并保存宏(以便将其全部放在一个位置,这是您将执行的组合键的完整列表,其中是您点击空格键的位置:
是当您按退出键时,< code>qaij0q
这会将宏保存在寄存器中a
)现在要播放宏,您需要执行
@
操作,然后执行保存宏的寄存器...所以在此示例中为@a
。现在第二行前面也有 2 个空格。宏还可以运行多次,因此如果我执行
3@a
,宏将运行 3 次,这样您就完成了。我喜欢像这样使用宏,因为它对我来说更直观,因为我可以准确地执行我想要它执行的操作,并且只需多次重播即可。
Another thing you could try is a macro. If you do not know already, you start a macro with
q
and select the register to save the macro... so to save your macro in registera
you would typeqa
in normal mode.At the bottom there should be something that says recording. Now just do your movement as you would like.
So in this case you wanted 2 spaces in front of every line, so with your cursor already at the beginning of the first line, go into insert mode, and hit space twice. Now hit escape to go to normal mode, then down to the next line, then to the beginning of that line, and press
q
. This ends and saves the macro(so that it is all in one place, this is the full list of key combinations you would do, where
<esc>
is when you press the escape key, and<space>
is where you hit the space bar:qai<space><space><esc>j0q
This saves the macro in registera
)Now to play the macro back you do
@
followed by the register you saved it in... so in this example@a
. Now the second line will also have 2 spaces in front of them.Macros can also run multiple times, so if I did
3@a
the macro would run 3 times, and you would be done with this.I like using macros for this like this because it is more intuitive to me, because I can do exactly what I want it to do, and just replay it multiple times.
我一直在寻找类似的解决方案,并使用这个变体
I was looking for similar solution, and use this variation