Neovim Line Manipulation: Copy and Move Like a Pro

Essential tips for efficient line manipulation in Neovim
January 18, 2025

Neovim line manipulation tips

Neovim command sequences visualization (click for full size)

After returning to Neovim recently, I've discovered some powerful techniques for manipulating lines that have significantly improved my workflow. Here's what makes these commands particularly interesting.

The Power of Line Range Commands

One of the most common scenarios in coding is needing to copy or move blocks of code from one location to another. While many developers reach for visual mode or rely on yanking and pasting, Neovim offers a more precise approach.

The basic syntax follows this pattern:

vim
:line1,line2t.

This command copies lines from line1 to line2 to your current cursor position. The beauty lies in its simplicity and precision.

Copy vs Move: Understanding the Difference

Here's where it gets interesting. The difference between copying and moving is just one character:

vim
:10,15t.    " Copy lines 10-15 to current position
:10,15m.    " Move lines 10-15 to current position

Why This Matters

What makes this approach superior to visual selection and yanking?

  1. No register pollution - Your yank register remains untouched
  2. Precise line targeting - No need to navigate to the exact lines
  3. Repeatable with dot - The command can be repeated easily
  4. Works across large distances - No scrolling required

Practical Examples

Let's say you're refactoring a function and need to move some validation logic from line 45-52 to your current position at line 20:

vim
:45,52m.

Or perhaps you want to duplicate a configuration block from lines 100-110:

vim
:100,110t.

Beyond Basic Line Manipulation

These commands become even more powerful when combined with other Vim motions:

vim
:10,20t$    " Copy lines 10-20 to end of file
:.,+5t0     " Copy current line plus next 5 to beginning
:-10,-5m.   " Move 10 lines up to 5 lines up to current position

Key Takeaways

  • Use :line1,line2t. for copying lines to current position
  • Use :line1,line2m. for moving lines to current position
  • These commands preserve your yank register
  • They work efficiently across large files without scrolling
  • Combine with other Vim motions for advanced manipulation

Notes

1. This technique works in both Vim and Neovim. 2. The dot (.) in the commands represents your current cursor position. 3. You can use marks instead of line numbers for even more flexibility.
Thank you for reading...