In Vim how do I effectively insert the same characters across multiple lines?

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

And make it looke like this

vendor_name
vendor_comment
vendor_phone
vendor_email

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

  • Select all 4 row lines of block by pressing V and then j 4 times.
  • Indent with >.
  • Go back one letter with h.
  • Go to block visual mode with ctrlv.
  • Select down 4 rows by pressing j 4 times. At this point you have selected a 4x1 visual block of whitespace (4 rows, 1 cols).
  • 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 <.
  • 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?

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

    Edit:

    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 )
    

    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 )
    

    in this example I would perform the vertical visual block over the . , then reinsert it back during insert mode, ie 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 .


  • Move the cursor to the n in name .
  • Enter visual block mode (ctrlv).
  • Press j three times.
  • Press I (capital i).
  • Type in vendor_ .
  • Press esc.
  • 演示该方法的微型屏幕录像

    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, eg for selecting a inside a tag block ( it ):

    显示它的文本对象的有用性的迷你屏幕录像


    Another approach is to use the . ( dot ) command in combination with I.

  • Move the cursor where you want to start
  • Press I
  • Type in the prefix you want (eg vendor_ )
  • Press esc.
  • Press j to go down a line
  • Type . to repeat the last edit, automatically inserting the prefix again
  • Alternate quickly between j and .
  • I find this technique is often faster than the visual block mode for small numbers of additions and has the added benefit that if you don't need to insert the text on every single line in a range you can easily skip them by pressing extra j's.

    Note that for large number of contiguous additions, the block approach or macro will likely be superior.


    I would use a macro to record my actions, then repeat it.

  • Put your cursor on the first letter in name.
  • Hit qq to start recording into the q buffer.
  • Hit i to go into insert mode, and type vector_ , then hit ESC to leave insert mode.
  • Now hit 0 to go back to the beginning of the line.
  • Now hit j to go down.
  • Now hit q again to stop recording.
  • You now have a nice macro.

    Type 3@q to execute your macro 3 times to do the rest of the lines.

    链接地址: http://www.djcxy.com/p/49462.html

    上一篇: 简单的VIM命令,你希望你早些时候知道

    下一篇: 在Vim中,我如何有效地在多行中插入相同的字符?