Vim: Copy Between Line Numbers Easily

Jerry Jul 18, 2026

Ever found yourself poring over an endless stream of code, wishing you could copy a specific segment, line by line, with just a few keystrokes? In Vim, this task becomes surprisingly straightforward with the help of visual mode andcommands designed precisely for such operations. Let's delve into the powerful world of Vim to learn how to copy from one line number to another seamlessly.

click on it to go to the website!!
click on it to go to the website!!

Before we dive in, ensure you're familiar with Vim's basic operations and visual mode. If not, fear not! There are plentiful resources online to guide you through these fundamentals. With that foundation set, let's begin.

How to Use Vim: A Guide to the Basics
How to Use Vim: A Guide to the Basics

Understanding Vim's Line Numbers

Vim allows you to navigate using line numbers, a feature particularly useful for targeting specific sections of your text. To view line numbers if they're not already visible, type "set number" and press Enter. To disable this feature, use "set nonumber".

Text Objects in Vim: A must have for programmers.
Text Objects in Vim: A must have for programmers.

Remember, these line numbers are relative to the cursor's current position. So, if you're on line 10, Vim will display lines from 10 upwards. To reset the line counter to 1, move your cursor to the beginning of your file with "gg".

Selecting Lines with Visual Mode

Vim quickies/ cheatsheet
Vim quickies/ cheatsheet

Vim's visual mode is your secret weapon for-line-by-line selection. To enter visual mode, press "v". To select entire lines, use "V" (capital V). Here's how you'd select lines 5 through 10:

  1. Position your cursor on line 5.
  2. Press "V" to enter visual line mode.
  3. Press "10G" to jump to line 10.

The lines from 5 to 10 are now selected. To delete these lines, press "d", or to copy them, press "y".

Navigation in Vim
Navigation in Vim

Copying Lines Between Line Numbers

Suppose you want to copy lines 15 to 20 to lines 8 to 12. Here's how you can do it:

  1. Position your cursor on line 15.
  2. Press "V" to enter visual line mode.
  3. Press "20G" to jump to line 20.
  4. Press "y" to copy the selected lines.
  5. Move your cursor to line 8.
  6. Press "20G" followed by "P" (capital P) to paste the copied lines.
3 Vim Modes: Normal, Insert and Visual
3 Vim Modes: Normal, Insert and Visual

The lines from 15 to 20 are now pasted after line 12. To paste before a line, press "P" instead of "p".

Using Ex Commands for Advanced Copying

Vi (Vim) Text base editor regular commands
Vi (Vim) Text base editor regular commands
How to Comment Multiple Lines in Vim Editor - Make Tech Easier
How to Comment Multiple Lines in Vim Editor - Make Tech Easier
*(❽❹❷⓿❺❶❶❸❹❷)✓Vaundy Loan Customer Care Number
*(❽❹❷⓿❺❶❶❸❹❷)✓Vaundy Loan Customer Care Number
the number line worksheet for kids to learn numbers and place value on them
the number line worksheet for kids to learn numbers and place value on them
Cheat Sheet: The Vim Linux Command Line Editor Cheat Sheet
Cheat Sheet: The Vim Linux Command Line Editor Cheat Sheet
How to Use Vim: A Guide to the Basics
How to Use Vim: A Guide to the Basics
How to Add Line Numbers in Google Docs [2024]
How to Add Line Numbers in Google Docs [2024]
an iphone screen showing the icons for different languages and numbers, including one in red
an iphone screen showing the icons for different languages and numbers, including one in red
the number line is shown in this worksheet
the number line is shown in this worksheet
VN codes 📌
VN codes 📌

For more complex copying operations, Vim's ex commands come into play. Ex commands are Bonaparte to visual mode's Napoleon - they pack a more powerful punch with slightly more complicated maneuvers.

Let's look at how to copy lines 19 to 25 and paste them after line 15 using an ex command:

  1. Position your cursor on line 19.
  2. Press quote """ to start a register mark.
  3. Press "25" to reach line 25.
  4. Press "'y25" to copy the selected lines.
  5. Move your cursor to line 15.
  6. Press quote """ followed by "25P" to paste the copied lines after line 25.

With ex commands, you can even define a range of lines with a single register. For instance, with "'19,25", you can copy lines 19 through 25 with "'19,25y".

Appending Copied Lines

To append copied lines instead of pasting them, replace "P" with "A" or "a". "A" appends after the line, while "a" appends before the line.

Remember, these commands are case-sensitive. "P" pastes before the cursor, while "p" pastes after the cursor. "A" appends text after the cursor, while "a" appends before the cursor.

Congratulations, you're now a line-copying virtuoso! With these techniques in your Vim toolkit, you can streamline your workflow and tackle editing tasks with unprecedented agility. Happy coding!