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
:help CTRL-O
- Episode #11: Using the changelist and the jumplist
:help ''
:help :t
:help :yank
- `:help ‘relativenumber’
- `:help {address}
- for more on Ex command ranges, see
:help 10.3