Entering Similar Lines of Text With vim

Grab the feed

School is a twelve-year jail sentence where bad habits are the only curriculum truly learned.

John Taylor Gatto

Recent comments

There's always something new about vim, or at least something that I learn everyday.

I was just coding a few form elements for Drupal and was getting tired of copying and pasting lines with similar text over a over again. I decided to take a look at vim's manual and found the magical CTRL+Y, exactly what I needed. Let's see how it works.

Imagine you have a line like this:

$form['a'] = array( '#type' => 'hidden', '#value' => $quickfile->title,);

and you need to create a few more like these, for example:

$form['b'] = array( '#type' => 'hidden', '#title' => t('Offers'),);

Notice that the first half of both lines is the same, except for the array's key name.

The first option to quickly add the second line is yanking with yy and pasting with p. Then you move over the new line and make changes as needed.

Your second option is to enter insert mode in the next line, the easiest way is using o, and then press CTRL+Y as many times as needed. Each press will insert the letter above the cursor.

In our example you will press CTRL+Y until reaching:

$form['

then enter b, instead of a, and continue with CTRL+Y until the first comma, then complete the line as usual.

CTRL+E does the same but copies the line below the cursor.

A simple tip to save you a few keystrokes and valuable time, two things that every vim user loves.

Keep your comments relevant, written in good English and don't spam. Let's create useful and valuable discussions. Markdown is welcome.

Add your comment