Neovim Line Manipulation: Copy and Move Like a Pro

Neovim Line Manipulation: Copy and Move Like a Pro

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:

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:

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:

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

Beyond Basic Line Manipulation

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

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

Source link