如何在vi中从光标位置开始插入一块空格? [英] How to insert a block of white spaces starting at the cursor position in vi?

查看:88
本文介绍了如何在vi中从光标位置开始插入一块空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有下面的一段文字,当前光标停留在第一个A

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?

目前我在视觉模式下复制第一列文本两次,并将整个两列替换为空格,这涉及> 5次击键,太麻烦了.

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.

约束:

抱歉,我没有把问题表述清楚,可能会造成一些混淆.

Sorry that I didn't state the question clearly and might create some confusions.

目标只是一个更大文件的一部分,所以如果可以指定从第一个A开始的行数和列数就好了.

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.

感谢@DeepYellow 和@Johnsyweb,显然>}>ap 都是我不知道的很棒的技巧,它们都可能是有效的在我澄清回答我的问题的具体要求之前,我先回答了我的问题,但无论如何,@luser droog 的答案是唯一可行的答案.谢谢大家!

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!

推荐答案

我会使用 :%s/^//

您还可以指定一系列行 :10,15s/^//

You could also specify a range of lines :10,15s/^/ /

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

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

Or use regular expressions for the locations :/A/,/D/>.

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

For copying code to paste on SO, I usually use sed from the terminal sed 's/^/ /' filename

我刚刚学会了一个新的技巧.您进入视觉模式 v,选择区域(使用常规移动命令),然后点击 : 这给你:

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:

:'<,'>

准备好让您输入上述命令的 command 部分,标记 '<'> 被自动设置到视觉选择的边界.

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.

选择和缩进当前段落:

vip>

vip:>

接着进入.

根据评论中的要求,您还可以在 any 元字符 \{n} 在行的中间添加空格>..

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}/& /

这会在每行左侧添加一个 14 个字符的空格.当然,% 可以替换为上述任何用于指定 ex 命令范围的选项.

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.

这篇关于如何在vi中从光标位置开始插入一块空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆