99

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?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • If you have to repeat it multiple times: https://superuser.com/questions/429917/repeat-last-normal-mode-command-including-moves-in-vim – toliveira Feb 24 '18 at 15:36

3 Answers3

148

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.

Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • 1
    Thanks! I should have figured out the count would apply to the move commands too. You made my day! –  Feb 07 '11 at 00:07
  • 2
    Since Vim 7.4 it's possible to see both relative and absolute line numbers at the same time by adding the following to .vimrc `set relativenumber` and `set number`. This will give relative line numbers with an absolute line number for the line your cursor is on. – Isaac Gregson Jul 29 '14 at 05:28
  • 2
    You can also move down a number of lines by simply typing the number followed by `enter` (for absolute line numbers, just preface it with a colon). If you're used to `j` and `k` and a normal keyboard layout, that may not be too helpful, but as an arrow-using Colemak user, `enter` is easier to reach than the Colemak `j` (the QWERTY `y` key ) – SnoringFrog Jun 09 '15 at 17:38
56

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).

Olhovsky
  • 5,466
  • 3
  • 36
  • 47
16

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.

Cilan
  • 13,101
  • 3
  • 34
  • 51
  • 4
    For moving forwards, `enter` works the same as `+` after a number, and is easier to reach most of the time – SnoringFrog Jun 09 '15 at 17:35
  • @SnoringFrog Yeah, but this is just so there can be some relationship between up and down; I don't mean for anyone to actually move up and down like this, because hjkl would be the fastest anyway. – Cilan Jun 10 '15 at 12:57
  • tiny addition when testing this locally: 10j and 10+ aren't identical: 10j keeps the column number, whereas 10+ goes to the first non whitespace character on that line, starting column agnostic – ShadowFlame Oct 18 '22 at 07:48