Vim:在多行中插入相同的字符 [英] Vim: insert the same characters across multiple lines

查看:102
本文介绍了Vim:在多行中插入相同的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时我想跨多行编辑特定的视觉文本块.

Sometimes I want to edit a certain visual block of text across multiple lines.

例如,我将采用如下所示的文本:

For example, I would take a text that looks like this:

name
comment
phone
email

让它看起来像这样

vendor_name
vendor_comment
vendor_phone
vendor_email

目前我的做法是......

Currently the way I would do it now is...

  1. V 然后按四次 j 选择块的所有 4 行.
  2. > 缩进.
  3. h 返回一封信.
  4. 使用 Ctrlv 进入块可视模式.
  5. j 四次选择向下四行.此时,您已选择了一个 4x1 的视觉空白块(四行一列).
  6. C.请注意,这几乎向左缩进了一列.
  7. 输入不带引号的 " vendor_".请注意我们必须放回的额外空间.
  8. Esc.这是我使用 Esc 退出插入模式的极少数次之一.Ctrlc 只会编辑第一行.
  9. 重复第 1 步.
  10. < 以另一种方式缩进.
  1. Select all 4 row lines of a block by pressing V and then j four times.
  2. Indent with >.
  3. Go back one letter with h.
  4. Go to block visual mode with Ctrlv.
  5. 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).
  6. Press C. Notice this pretty much indented to the left by one column.
  7. Type out a " vendor_" without the quote. Notice the extra space we had to put back.
  8. 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.
  9. Repeat step 1.
  10. Indent the other way with <.

如果单词前至少有一列空格,我不需要缩进.如果我不必用 c 清除视觉块,我就不需要空格.

I don't need to indent if there is at least one column of whitespace before the words. I wouldn't need the whitespace if I didn't have to clear the visual block with c.

但是如果我必须清除,那么有没有办法在不创建所需的缩进空格的情况下执行我上面执行的操作?

But if I have to clear, then is there a way to do what I performed above without creating the needed whitespace with indentation?

另外,为什么一次编辑多行只能通过使用 Esc 而不是 Ctrlc 退出插入模式?

Also why does editing multiple lines at once only work by exiting out of insert mode with Esc over Ctrlc?

这是一个更复杂的例子:

Here is a more complicated example:

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

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..希望现在您可以看到这种方法的缺点.我只能选择一列文本在垂直位置都相同.

In this example I would perform the vertical visual block over the ., and then reinsert it back during insert mode, i.e., type .whatever.. Hopefully now you can see the drawback to this method. I am limited to only selecting a column of text that are all the same in a vertical position.

推荐答案

  1. 将光标移动到name中的n.
  2. 进入可视块模式(Ctrlv).
  3. j 三次(或 3j).
  4. I(大写 i).
  5. 输入vendor_.注意:它只会更新 first 行中的屏幕 - 直到按下 Esc (6.),此时所有行都将被更新了.
  6. Esc.
  1. Move the cursor to the n in name.
  2. Enter visual block mode (Ctrlv).
  3. Press j three times (or 3j).
  4. Press I (capital i).
  5. Type in vendor_. Note: It will only update the screen in the first line - until Esc is pressed (6.), at which point all lines will be updated.
  6. Press Esc.

必须使用大写的I 而不是小写的i,因为小写的i 被解释为文本对象,它本身就很有用,例如用于在标签块内进行选择 (it):

An uppercase I must be used rather than a lowercase i, because the lowercase i is interpreted as the start of a text object, which is rather useful on its own, e.g. for selecting inside a tag block (it):

这篇关于Vim:在多行中插入相同的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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