Copy/Move/Delete text without the cursor movement in Vim

:: Mar 2, 2021 :: vim L2 ::

## Copy lines without cursor movement

Your cursor is on line 10. You want to paste line 20 to one line below your current cursor position. Here’s how to do that using co[py] command: :20co.

You can also use ranges. Let’s say your cursor is on line 10. You want to paste text from line 20 to 25 under line 10. Here’s how: :20,25co10

It gets even better: :t is an alias of the co[py] command, so you could save some keystrokes. You could run the commands from above and achieve the same result if you’d replace co with t in them.

This command works with relative line numbers as well. For example, to paste the line, which is 10 lines above your current line, to a line below your current position: -10t.

One last example: if you’re at line 45, :35,t. will make a duplicate of lines 35 to your cursor (that is, from 35 to 45 inclusive) and put it after your current cursor.

So, imagine this case: you have a function and your cursor is one line below it. You also have relative line numbers enabled. You see that the function starts 15 lines above your current line. To make a copy of the entire function and place it after your current line, you could run: :-15,t.

Note: My book Mastering Vim Quickly: From WTF to OMG in no time helped thousands of people to drastically improve their Vim skills within an hour! If you'd like to do the same, look it up: ebook + screencasts | paperback

## Move lines without cursor movement (that much)

The usage of command m[ove] is similar to co[py] command. For example, to move a line 6 to line 28, you’d run: :6m28

It also supports ranges and relative lines.
Here’s an example using both: :-10,-5m+7

This command would take five lines which are located between lines 10 and 5 above your current position, and move them to 7th line under your current position.

After this command, the position of your cursor will change. In order to come back to the original location where your cursor was before running this command, simply hit ''. Surely, the cursor moves. But, this workflow saves you from moving your cursor around to perform the visual selection.

## Delete lines without cursor movement (that much)

Wouldn’t it be cool to be able to delete lines without moving your cursor?

Similar to :co[py] and :m[ove] commands, you can also run :d[elete] command to delete lines without jumping to those lines.

For example, to delete lines 5 to 10, run: :5,10d

However, this command would leave the cursor at the deleted line location, so you’d need to use '' to jump back to the previous position.

You're welcome to join my private email list or follow me on Twitter.