Neovim Line Manipulation: Copy and Move Like a Pro
文章介绍了在 Neovim 中通过行范围命令高效复制和移动代码块的方法,展示了其在精准操作、避免注册污染和跨大文件处理中的优势,并提供了实际应用示例和组合技巧。 2025-1-18 12:0:0 Author: danielmiessler.com(查看原文) 阅读量:0 收藏

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:

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:

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:

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

文章来源: https://danielmiessler.com/blog/neovim-line-manipulation-tips
如有侵权请联系:admin#unsafe.sh