Your destination for complete Tech news

How to duplicate a line in vim?

4.43K 0
2 min read

Vim is one of the most powerful text editors, loved by developers, sysadmins, and Linux enthusiasts. But when you’re new, even simple tasks like duplicating a line can feel confusing.

The good news: there are several quick ways to copy and paste lines in Vim. This article will walk you through basic, advanced, and time-saving methods to duplicate lines efficiently.

Method 1: Yank and Paste (Most Common)

The simplest way is to yank (copy) the line and paste it.

  1. Press Esc to enter Normal mode
  2. Move your cursor to the line you want to duplicate
  3. Type: yy This yanks (copies) the whole line.
  4. Then press: p This pastes the line below the current one.

If you want to paste above, use P instead of p.

Method 2: Use the :t. Command

Another neat trick is using Vim’s built-in :t command.

  1. Place your cursor on the line to duplicate
  2. Type: :t. and press Enter.

This copies the current line (t) and places it below the same line (.).

Example: If your cursor is on line 5, :t. will duplicate line 5 directly below it.

You can also use numbers:

  • :t6 → copies current line and pastes it after line 6
  • :.,.+2t. → duplicates the current line and the next 2 lines

Method 3: Visual Mode (Great for Multiple Lines)

If you want to duplicate more than one line:

  1. Press Esc to go to Normal mode
  2. Press V (capital V) to start Visual Line mode
  3. Move up or down to select multiple lines
  4. Press y to yank (copy)
  5. Press p to paste below

This is great for duplicating code blocks.

Method 4: Create a Custom Mapping

If duplicating lines is something you do often, you can save time with a custom mapping in your .vimrc:

nnoremap <Leader>d yyP

Now pressing <Leader>d (usually \d) will duplicate the current line above it instantly.

You can customize it to paste below instead by changing it to yyP.

Advanced Tips

  • Use registers: "ayy stores the line in register a, then "ap pastes it. This way you won’t overwrite your default yank register.
  • Repeat counts: 2yy yanks two lines, then p pastes both.
  • Undo mistakes: press u if you accidentally duplicated too much.
  • Indentation issues: check your autoindent or smartindent settings if pasted lines shift incorrectly.

FAQ: Duplicating Lines in Vim

Q: Can I duplicate without moving the cursor?
Yes. Using :t. keeps your cursor on the same line after duplication.

Q: Does this work in Neovim too?
Yes. Neovim inherits the same commands from Vim.

Q: Can I duplicate a line multiple times quickly?
Yes. For example, type yy and then 3p to paste the line three times below.


Conclusion

Duplicating lines in Vim is simple once you know the commands. You can use:

  • yy + p → fast and easy
  • :t. → great for precision
  • Visual Mode → perfect for multiple lines
  • Mappings → automate and speed up your workflow

Mastering these techniques will save you time and make editing in Vim much more efficient.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.