如何在vi中从光标位置开始插入一块空格?

发布于 2024-12-07 03:31:06 字数 601 浏览 0 评论 0原文

假设我有下面一段文字,当前光标停留在第一个 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 技术交流群。

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

发布评论

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

评论(9

泛泛之交 2024-12-14 03:31:06

我会使用 :%s/^/ /

您还可以指定行范围 :10,15s/^/ /

或相对范围 :。 ,+5s/^/ /

或者对位置使用正则表达式:/A/,/D/>

为了复制代码以粘贴到 SO 上,我通常从终端使用 sed sed 's/^/ /' filename


快捷方式

我刚刚学到了一个新技巧。您进入视觉模式v,选择区域(使用常规移动命令),然后点击,这将为您提供:

:'<,'>

准备好输入只是上述命令的 command 部分,标记 '<'> 自动设置为视觉选择的边界。

选择并缩进当前段落:

vip>

vip:>

回车

编辑:

根据评论中的要求,您还可以在 any 上使用正则表达式量词 \{n} 在行的中间添加空格> 元字符.

:%s/^.\{14}/& /

这会在每行左侧添加一个 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:

vip>

or

vip:>

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

:%s/^.\{14}/& /

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.

那小子欠揍 2024-12-14 03:31:06

当在第一个 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, press I (insert mode with capital i), and apply any changes I want for the first line. Leaving visual mode esc 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.

早乙女 2024-12-14 03:31:06

我会使用 >}

其中...

  • >:右移并
  • < a href="http://vimdoc.sourceforge.net/htmldoc/motion.html#object-motions">}:表示直到段落结束

希望这会有所帮助。

I'd use >}.

Where...

  • >: Shifts right and
  • }: means until the end of the paragraph

Hope this helps.

绝對不後悔。 2024-12-14 03:31:06
  1. Ctrl + v (进入可视模式)
  2. 使用箭头键选择行
  3. Shift + i (带您进入插入模式)
  4. 按空格键或您想要在所选行前面键入的任何内容。
  5. 保存更改(使用 :w),现在您将看到所有选定行中的更改。
  1. Ctrl + v (to enter in visual mode)
  2. Use the arrow keys to select the lines
  3. Shift + i (takes you to insert mode)
  4. Hit space keys or whatever you want to type in front of the selected lines.
  5. Save the changes (Use :w) and now you will see the changes in all the selected lines.
山川志 2024-12-14 03:31:06

我会喜欢尼古。另一种解决方案是使用 :normal:

  1. 进入 VISUAL-LINE 模式
  2. 3jjjj/D 选择行
  3. :norm I,正确的范围('<,'>) 自动插入

:normal 对于这种特定情况可能有点矫枉过正,但有时您可能想要执行一堆复杂的操作一系列线路。

I would do like Nigu. Another solution is to use :normal:

  1. <S-v> to enter VISUAL-LINE mode
  2. 3j or jjj or /D<CR> to select the lines
  3. :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.

忆梦 2024-12-14 03:31:06

您可以在可视模式下选择行,然后键入 >。这假设您已将制表符设置为插入空格,例如:(

setl expandtab
setl shiftwidth=4
setl tabstop=4

将 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.:

setl expandtab
setl shiftwidth=4
setl tabstop=4

(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.

泪冰清 2024-12-14 03:31:06

假设您要移动一段代码:

  • 设置每个移动命令使用的空格数, :set shiftwidth=1,默认为 8。
  • Ctrl+v在适当的位置并将光标向上 k 或向下 j 移动以选择某个区域。
  • > 移动方块,按 . 重复该操作,直到所需位置(如果光标丢失,请按 h返回>b)。

移动块代码

Let's assume you want to shift a block of code:

  • setup the count of spaces used by each shift command, :set shiftwidth=1, default is 8.
  • press Ctrl+v in appropriate place and move cursor up k or down j to select some area.
  • press > to shift the block and . to repeat the action until desired position (if cursor is missed, turn back with h or b).

move block of code

您可以尝试的另一件事是宏。如果您还不知道,请使用 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 register a you would type qa 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 register a )

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.

携君以终年 2024-12-14 03:31:06

我一直在寻找类似的解决方案,并使用这个变体

VG:norm[N]I

N = numbers of spaces to insert. 
V=Crtl-V 
*** Notice ***  put space immediate after I.

I was looking for similar solution, and use this variation

VG:norm[N]I

N = numbers of spaces to insert. 
V=Crtl-V 
*** Notice ***  put space immediate after I.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文