Moving (efficiently) vertically in Vim

27

10

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)

janoChen

Posted 2010-01-23T19:00:09.187

Reputation: 561

Answers

29

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.

Alison R.

Posted 2010-01-23T19:00:09.187

Reputation: 3 360

5And zb and zt move screen on bottom and top respectively – Jani Hartikainen – 2010-01-23T19:44:17.350

11

  • 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

Bandi-T

Posted 2010-01-23T19:00:09.187

Reputation:

9

  • { } 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

PurplePilot

Posted 2010-01-23T19:00:09.187

Reputation: 191

6

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

James Thompson

Posted 2010-01-23T19:00:09.187

Reputation: 193

+1 for <CTRL>-F and <CTRL>-B, although I tend to "cheat" and use <PgUp> and <PgDown> almost as often! – technomalogical – 2010-01-25T20:06:48.643

4

try: :he motion.txt and :he up-down-motions

It has the whole list of possible motions up down

michael

Posted 2010-01-23T19:00:09.187

Reputation: 101

3

You can also make jumps to specific line in document by issuing Ngg where N is line number you want to jump.

mloskot

Posted 2010-01-23T19:00:09.187

Reputation: 250

1you mean NG... – Peter – 2010-01-23T19:28:32.417

1I use Ngg, too (both variants work) – soulmerge – 2010-01-23T19:42:39.123

1As well as :N – viraptor – 2010-01-23T19:45:50.350

3

You should give Lokaltog's easymotion plugin a try: http://www.vim.org/scripts/script.php?script_id=3526

Easymotion plugin at work

f3lix

Posted 2010-01-23T19:00:09.187

Reputation: 571

easymotion is really good but I don't know why to make it work for the part above the cursor. It only highlights the one below the cursor. – Dzung Nguyen – 2012-06-12T12:29:52.663

2

as previously mentioned H,M,L(as in high, middle, low) lower case h j k l are the typical console game commands for left, down, up, right and work the same in vi (j,k work in gmail if you enable key commands)

schemathings

Posted 2010-01-23T19:00:09.187

Reputation:

1

<Ctrl>+D - down

<Ctrl>+U - up

sml

Posted 2010-01-23T19:00:09.187

Reputation: 1 582

That's a half a page scroll. For full page scroll use <c-f> and <c-b> – Al.G. – 2016-02-28T08:58:29.797

1

In addition to the regular vertical motions, I use a modified version of https://stackoverflow.com/questions/4946421/vim-moving-with-hjkl-in-long-lines-screen-lines. I usually move with the arrow keys, not hjkl (looong time user of other editors before I found Vim, too used to arrows/Home/end/PgUp/PgDn to switch, even after 5+ years of exclusive Vim use).

function! ScreenMovement(movement)
   if &wrap
      return "g" . a:movement
   else
      return a:movement
   endif
endfunction
map <expr> <C-Down> ScreenMovement("j")
map <expr> <C-Up> ScreenMovement("k")
map <expr> <C-Home> ScreenMovement("0")
map <expr> <C-End> ScreenMovement("$")

By holding Ctrl I can go visually to start/end of or up/down display rows. For me this is more efficient than the alternatives, at least.

Almost forgot: I needed to modify .Xdefaults for it to work in specifically urxvt, but that is probably an off-topic issue (with answers on Google).

Daniel Andersson

Posted 2010-01-23T19:00:09.187

Reputation: 20 465