Move cursor x lines from current position in vi/vim

Is there a way to move the cursor a relative amount of lines in vi/vim? Say you have the cursor 10 lines under a block of code you want to remove. If you have the line numbers shown in relative order, it would be nice to have a "jump 10 lines up command" that would take you there.

Or perhaps it's better to have the absolute line numbers shown and go xgg where x is the line number?


Solution 1:

Yep, of course there's a way. j and k move down and up one line, so 10j and 10k move down and up ten lines. You can repeat any motion by putting a number before it.

You might also want to set relativenumber if this is something you do a lot of - it'll help save you counting by printing line numbers relative to the current line, instead of absolute numbers.

Solution 2:

Moving 10 lines up and down might not suit your task as well as other options. Consider other movements:

Ctrlf, Ctrlb page forward and back.

}, { move forward and back by one paragraph.

You can write rules in your vimrc to bind 10j to a key, say J to move down 10 lines by adding the following line to your vimrc file: map <S-j> 10j

However you'd be overwriting the useful existing J command (join two lines). Finding a well positioned unused key combination for 10j/10k might be difficult, so I suggest using the existing movements that I mentioned.

You may also want to know that you can move backwards to a word that you see by doing: ?someword and forward to a word you see by doing /someword. These are going to be faster than trying to move up/down 10 lines and then repositioning your cursor to the exact location. If you cant think of a simple search string for the line in question, you can always go to the line number as you said (xgg).

Solution 3:

I was messing with vim and I noticed - moves you up and + moves you down, so you can:

10-

or you could use k since you're most likely used to hjkl cursor movement.