Learn Vim at your own pace with my self-study Core Vim Course.

Learn more

Learn Vim at your own pace with my self-study Core Vim Course.

Long-range line duplication

#40

Run time:

When writing code, we can often save time by duplicating a line then changing one or two parts of that line to make it suit our purposes. In this episode, we’ll compare a few techniques for duplicating lines, and we’ll see that the :copy Ex command is well suited to this task.

Shownotes

The brief

In the video, we tackle a simple problem. With our cursor on line 16 of this file:

#sponsors_feature, #sponsors_index {
  width: 120px;
}
#sponsors_index {
  position: absolute;
  top: -2px; right: 0;
}
#sponsors_index h2 {
  background: #fff url('/images/components.png') -362px -579px;
}
#sponsors_feature {
  position: absolute;
  top: 136px; left: 20px;
}
#sponsors_feature h2 {
  text-indent: -99999px;
  margin-bottom: 0px;
}

Copy line 9 and place a duplicate below line 16, to produce this:

#sponsors_feature, #sponsors_index {
  width: 120px;
}
#sponsors_index {
  position: absolute;
  top: -2px; right: 0;
}
#sponsors_index h2 {
  background: #fff url('/images/components.png') -362px -579px;
}
#sponsors_feature {
  position: absolute;
  top: 136px; left: 20px;
}
#sponsors_feature h2 {
  text-indent: -99999px;
  background: #fff url('/images/components.png') -362px -579px;
  margin-bottom: 0px;
}

Normal mode solutions

We start with a naïve solution:

kkkkkkk
yy
jjjjjjj
p

Our first refinement speeds up navigation using the goto line command (:help G), and the jumplist:

9G
yy
<C-o>
p

Ex command solutions

An alternative way to goto line 9 would be :9. We can also use the number as a range for the :yank command. The cool thing about this technique is that it doesn’t move our cursor, so we can cut the number of steps in half:

:9yank
p

But we can still do better. Vim has another Ex command that combines the yank and put operations into one: the :copy command. This is the longhand form:

:9copy16

We can compress this down to just three characters:

:9t.

The :t command is simply an alias for :copy. When used in an {address} the dot symbol stands for the current line.

The :copy command

Here are some more examples of how the :copy command can be used:

command action
:9t. copy line 9 placing a duplicate below the current line
:t5 copy the current line placing a duplicate below the line 5 (and moving the cursor)
:-7t. copy the line 7 before the current cursor position placing a duplicate below the current line
:+4t. copy the line 4 after the current cursor position placing a duplicate below the current line
:9,11t. copy the lines 9 to 11 placing the duplicate lines below the current cursor position

Further reading

Comments

Level-up your Vim

Training

Boost your productivity with a Vim training class. Join a public class, or book a private session for your team.

Drew hosted a private Vim session for the shopify team that was one of the best workshops I have ever attended.

John Duff, Director of Engineering at Shopify

Publications

Make yourself a faster and more efficient developer with the help of these publications, including Practical Vim (Pragmatic Bookshelf 2012), which has over 50 five-star reviews on Amazon.

After reading it, I've switched to vim as my default editor on a daily basis with no regrets. ★★★★★

Javier Collado

Learn to use Vim efficiently in your Ruby projects

In association with thoughtbot, one of the most well respected Rails consultancies in the world, I've produced a series of screencasts on how to make navigating your Ruby projects with Vim ultra-efficient. Along the way, you’ll also learn how to make Ruby blocks a first-class text object in Vim. This lets you edit Ruby code at a higher level of abstraction. Available to buy from thoughtbot..