有时我想跨多行编辑某个可视文本块。

例如,我会取一个看起来像这样的文本:

name
comment
phone
email

让它看起来像这样

vendor_name
vendor_comment
vendor_phone
vendor_email

现在我要做的是…

Select all 4 row lines of a block by pressing V and then j four times. Indent with >. Go back one letter with h. Go to block visual mode with Ctrlv. Select down four rows by pressing j four times. At this point you have selected a 4x1 visual blocks of whitespace (four rows and one column). Press C. Notice this pretty much indented to the left by one column. Type out a " vendor_" without the quote. Notice the extra space we had to put back. Press Esc. This is one of the very few times I use Esc to get out of insert mode. Ctrlc would only edit the first line. Repeat step 1. Indent the other way with <.

如果单词前至少有一列空白,我就不需要缩进。如果我不需要用c清除可视块,我就不需要空白。

但是如果我必须清除,那么是否有一种方法来做我上面所做的而不创建所需的缩进空白?

还有为什么一次编辑多行只能通过退出插入模式与Esc在Ctrlc?


这里有一个更复杂的例子:

name    = models.CharField( max_length = 135 )
comment = models.TextField( blank = True )
phone   = models.CharField( max_length = 135, blank = True )
email   = models.EmailField( blank = True )

to

name    = models.whatever.CharField( max_length = 135 )
comment = models.whatever.TextField( blank = True )
phone   = models.whatever.CharField( max_length = 135, blank = True )
email   = models.whatever.EmailField( blank = True )

在这个例子中,我将在.上执行垂直可视块,然后在插入模式下重新插入它,即输入.whatever..希望现在你能看到这种方法的缺点。我只能选择一列在垂直位置相同的文本。


当前回答

Ctrl + v进入可视块模式 使用向上和向下箭头选择线条 输入小写的3i(按小写的I三次) I(按大写I,进入插入模式。) 写下你想要添加的文本 Esc 按下箭头

其他回答

如果整个文件都需要更改,

:1,$s/^/vendor_/

如果只需要更改几行,

转到需要更改的第一行,给出命令

:.,ns/^/vendor_/

将n替换为块中最后一行的行号。

Or,

:.,+ns/^/vendor_/

将n替换为需要更改的行数减去1。

Use Ctrl+V to enter visual block mode
Move Up/Down to select the columns of text in the lines you want to comment.
Then hit Shift+i and type the text you want to insert.
Then hit Esc, wait 1 second and the inserted text will appear on every line

我来这里粘贴许多行已经复制的字符串。当用y复制时,我们可以粘贴,在插入模式下,按Ctrl+r,然后按“”。这将有相同的结果,在正常模式和按p。这被称为从注册表粘贴。

假设缓冲区中有以下文本:

vendor_something

text
to_receive
the_paste
pattern

然后我们可以把光标指向vendor_中的v,按下v,使用l向右移动,直到选择我们想要粘贴在下面的文本中的下划线符号。在此之后,我们可以将光标指向“text”的开头(vendor_something后面两行)并按Ctrl+v。然后我进入插入模式,我们按3j Ctrl+r”Esc。这个序列的结果将是:

vendor_something

vendor_text
vendor_to_receive
vendor_the_paste
vendor_pattern

假设你有这个文件:

something

name
comment
phone
email

something else
and more ...

你需要在“name”、“comment”、“phone”和“email”前面添加“vendor_”,而不管它们出现在文件中的哪个位置。

:%s/\<\(name\|comment\|phone\|email\)\>/vendor_\1/gc

c标志将提示您确认。如果你不想要这个提示,你可以去掉它。

另一种方法是使用。(dot)命令与i结合使用。

将光标移动到想要开始的位置 按我 输入你想要的前缀(例如vendor_) 按esc。 按j向下一行 类型。若要重复上次编辑,请再次自动插入前缀 在j和。

我发现对于少量的添加,这种技术通常比视觉块模式更快,并且有额外的好处,如果你不需要在范围内的每一行插入文本,你可以通过按下额外的j轻松跳过它们。

注意,对于大量的连续添加,块方法或宏可能会更好。