Relative Copy in vim

May 13, 2012

It surprises me that I’ve not found or used this before!

In vim, you can use relative line numbers to copy and paste code(it can actually be used for other commands as well but I stick to one command at a time).  Using this command:

:-5t.

Will copy the line of code that is 5 lines above your current cursor to your current position.

This line:

:-10, -8 t.

Will copy from 10 lines above your cursor to 8 lines above your cursor and put it to your current position.

This is incredibly handy when trying to rearrange text and prevents you from needing to move to the line you need to copy, only to move back to where you need to paste it.

Discussion, links, and tweets

I'm a developer at thoughtbot.

Follow me on Twitter. I rarely post anything but it can't hurt to try.