开发者

Moving between lines in VIM

Let's say I have a file with N lines. I'm at lin开发者_运维百科e 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?


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


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


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


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)


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜