3

Let's say I have a file with N lines. I'm at line X and I'd like to move to line Y, where both X and Y are visible on screen. I can do that by typing :Y<cr>, but if Y>99 that's a lot of typing. I can also do abs(Y-X)[kj] (move up or down by abs(Y-X)), but for big X,Y computing this difference mentally isn't so easy.

Is there a way to exploit the fact, that both X,Y are visible on screen and move between X and Y fast?

pbp
  • 1,461
  • 17
  • 28

6 Answers6

8

You can :set relativenumber which does that Y-X computing for you (only in Vim >= 7.3).

dmedvinsky
  • 8,046
  • 3
  • 31
  • 25
3

You can use H, M or L to go the top, middle and bottom of the screen.

Facundo Casco
  • 10,065
  • 8
  • 42
  • 63
2

Perhaps you can make use of H, M, or L.

These keys jump the cursor to display lines:

H    "Home" top of screen
M    "Middle" middle of screen
L    "Last" last line of screen

With a count, they offset: 4L would go to the third line above the last (1L is the same as just L).

Personally, I make heavy use of the m command to mark a line for navigation. From where I am now, hit mq to mark the position with label q; then navigate to another line, and ma to mark it with label a; and from then on I can hit 'q to jump to position q and 'a to jump to position a. (q and a are arbitrary; I use those mostly due to their position on a QWERTY keyboard.)

One you have the marks, you can use them for commands. To delete from the current position to the line marked with q, you just use: d'q

There is a variant, where instead of single quote you use back quote. This takes you to the exact position on the line where you placed the mark; the single quote uses the start of the line.

Those marks work even for ex (command line) commands. To limit search and replace to a specific set of lines, I mark the beginning and end lines respectively with labels b and e, and then do my search and replace like so:

:'b,'es/foo/bar/g
steveha
  • 74,789
  • 21
  • 92
  • 117
2

Dropping my dime in the pond:

I find that traversing code is exceptionally easy with text objects. I rarely do use jk/JK for larger jumps any more. Instead I navigate for whitespace lines using { and }

Since on any one screen there are usually only so-many whitespace delineations (and they are very easily visually recognized and counted), I find that e.g.

   3}j

lands me on the intended line a lot more often than, e.g., a guesstimated

     27j

To top it all, many 'brace-full' programming languages have opening braces at the start of functions. These can be reached with [[ resp. ]]. So sometimes it is just a matter of doing, e.g.

   2[[}

(meaning: go to start of previous function, after the first contiguous block of lines)

sehe
  • 374,641
  • 47
  • 450
  • 633
  • It is a slight bother when team members use editors that insert tons of extraneous whitespace; `{` won't stop at (or count) a line which consists of some amount of whitespace. `autocmd BufWritePre *.{cpp,etc} %s/\s\+$//e` to the rescue! – dash-tom-bang Oct 03 '11 at 21:37
  • @dash-tom-bang: that always had me fascinated: what editors _do_ that?! I've come to the conclusion that the only _editors_ with that problem are invariably sitting behind the keyboard :) - but maybe you're in on a secret I don't know about? – sehe Oct 03 '11 at 21:39
  • If you hit `` on an otherwise empty line in (some) other editors that do some form of autoindent, you're often left with the indent on that line. Vim, on the other hand, removes said whitespace. PEBCAK explains some issues, but not all. :) I couldn't tell you which ones do it though because people here use all sorts of different things free and not free. – dash-tom-bang Oct 03 '11 at 21:46
0

My version of VIM lets you guestimate a number immediately before hitting J or K to go that many lines.

15K goes up 15 lines

Tallboy
  • 12,847
  • 13
  • 82
  • 173
  • That's what I meant by "abs(Y-X)[kj]" – pbp Sep 29 '11 at 19:55
  • @pbp, no, actually, when you are using vim for a while, you just don't counting in absolute numbers, you are just trying to get the approximate amount of line you need to jump. – shabunc Sep 29 '11 at 19:57
0

The tougher vimmer you are becoming, the bigger amount of lines you can count at first glance. Don't know, maybe there are some clever techniques, but I just type something like 17k/23j and so on.

also, searching some word on the string you want to jump works.

also, zz (center screen) is sometimes helpful in this cases.

shabunc
  • 23,119
  • 19
  • 77
  • 102