29

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)

0

10 Answers 10

32

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.

1
  • 5
    And zb and zt move screen on bottom and top respectively Jan 23, 2010 at 19:44
12
  • { } 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

0
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
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

1
  • +1 for <CTRL>-F and <CTRL>-B, although I tend to "cheat" and use <PgUp> and <PgDown> almost as often! Jan 25, 2010 at 20:06
4

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

It has the whole list of possible motions up down

3

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

3
  • 1
    you mean NG...
    – Peter
    Jan 23, 2010 at 19:28
  • 2
    I use Ngg, too (both variants work)
    – soulmerge
    Jan 23, 2010 at 19:42
  • 2
    As well as :N
    – viraptor
    Jan 23, 2010 at 19:45
3

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

Easymotion plugin at work

1
  • 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. Jun 12, 2012 at 12:29
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)

2

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

1

<Ctrl>+D - down

<Ctrl>+U - up

1
  • That's a half a page scroll. For full page scroll use <c-f> and <c-b>
    – Al.G.
    Feb 28, 2016 at 8:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .