Trusted answers to developer questions

How to prepend a set of lines with Vim

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

Have you ever faced a situation where you just wanted to have a set of characters prepended to a set of lines in one of your files, and wished that there was an easy way to do it? Well, Vim gives you a very simple way of doing this.

First, let’s create a file with some contents. I have a main.txt with the following contents:

Hi
This is
Testing
insertion of
word at the
beginning of all lines

Now, do the following and see what happens:

  1. Use Ctrl+V to select the first column of text in the lines you want to comment on.
  2. Then, hit Shift+I and type the text you want to insert.
  3. Finally, hit Esc, wait 1 second, and the inserted text will appear on every line.

RELATED TAGS

vim
Did you find this helpful?