Neovim Line Manipulation: Copy and Move Like a Pro
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:
:10,15t. " Copy lines 10-15 to current position
:10,15m. " Move lines 10-15 to current position
1
2
Why This Matters
What makes this approach superior to visual selection and yanking?
- No register pollution – Your yank register remains untouched
- Precise line targeting – No need to navigate to the exact lines
- Repeatable with dot – The command can be repeated easily
- 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:
: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
1
2
3
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