Moving (efficiently) vertically in Vim

When I want to reach to a certain part of a word in Vim (horizontally) I simply press w, b, f or t (something). Is there a good way of doing this vertically? For instance, I want to get to a certain word or a letter of a line that is below or above the line I'm currently in.

PS: Without using :(line number)


Solution 1:

H, M and L will move your cursor to the top, middle and bottom of the screen, respectively. Subsequently, you can hit zz to center the screen on the line your cursor is on.

Solution 2:

  • you can use the up/down arrow to move one line
  • you can type a number and then use the up/down arrow to move up/down that many lines
  • small g jumps to the first line of the document
  • capital G jumps to the last line of the document
  • etc.

there are a gazillion of other different ways, vim is very flexible in this respect. I would recommend to get a vim cheat sheet first for basic commands, and then as you have time, go through the vim documentation on this subject.

Link to one cheat sheet: Cheat sheet

Also:

  • / + string + (Enter) searches for the next occurrence of string from the cursor forward to the end of the document
  • ? - does the same but from the cursor backwards towards the beginning of the document

Solution 3:

  • { } will get you to the beginning / end of the next paragraph
  • ( ) the same for sentences
  • ^ $ the first, last character of line

Quick reference / cheat sheet here

Solution 4:

I jump around by searching for unique strings where I want to go. In Vim, you can do this with:

/search-string

I usually scroll forwards and backwards using n and N once I've entered a search.

I also scroll using Ctrl-F and Ctrl-B to page through code quickly. But I use the search trick most often. Sometimes you can get lost when going through search results with n and N. A good command for orienting yourself is Ctrl-G, which prints out something like this at the bottom of the Vim screen:

"script.pl" line 219 of 328 --66%-- col 19-23