How to insert text at beginning of a multi-line selection in vi/Vim

Jordan Parmer picture Jordan Parmer · Oct 31, 2008 · Viewed 366.5k times · Source

In Vim, how do I insert characters at the beginning of each line in a selection?

For instance, I want to comment out a block of code by prepending // at the beginning of each line assuming my language's comment system doesn't allow block commenting like /* */. How would I do this?

Answer

pixelbeat picture pixelbeat · Oct 31, 2008
  • Press Esc to enter 'command mode'
  • Use Ctrl+V to enter visual block mode
  • Move Up/Downto 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.

For further information and reading, check out "Inserting text in multiple lines" in the Vim Tips Wiki.