Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most efficient way to reach a spot on sight on VIM?

Suppose you are on line 640 and notice the following on line 671:

if (jumps_over(quick_fox[brown],the_lazy_dog)) the_lazy_dog.bark(); 

What would be the most key efficient way to navigate to "bark"?

like image 277
MaiaVictor Avatar asked Aug 11 '12 23:08

MaiaVictor


1 Answers

This is a common misconception about Vim: Vim is not designed to be efficient.

Vim is designed to use small building blocks in repeatable, often complex combinations, to achieve your goal. It is not an inherently efficient process, but for a subset of complex tasks, it's useful. For example, [motion][macro], or [count][command], these are small building blocks combined together in chains. Clicking on the screen is the most efficient movement, but might not be useful in a subset of a large text manipulation task where you have to do it thousands of times.

Here are all the ways I normally use that I can remember, you can see none of these are efficient uses of movement nor my cognitive processing power. (IDEs do things efficiently by taking out all the work listed below).

Once on the line I would personally use f.l since a quick scan shows that's a unique character. Or T. if you're past it. If there ends up being more than one . I would use ; to repeat the find until I got to it. Or, mash w or W until I got close, since trying to guess where lowercase w will take you when punctuation is involved is basically impossible in Vim.

If you have :set relativenumber on you can see how many lines away it is, and type nj to go there, then type f. to jump next to it to it

If you have :set number on you can type (since you'd know the line number) 671G (or :671Enter) f.

If you had marked that line with, say ma you could jump to the line with 'a or the exact char with `a (but most keyboards make ` useless to reach :( You probably won't use this one if it's a random look-need-to-fix jump.

If you have EasyMotion installed you could type leaderleaderfbsubchar to jump right to it

You could search for it as @kev suggests and use nN to bounce around until you get it. If bark is common but the_lazy_dog is unique you could do /dog.b/eEnter (e places cursor at end of match) and pray to the Vim gods that your search was specific enough to only match that line. That's more of a fun guessing game.

Depending on where the line is on the screen, you could use (n offset)H, M or L (high middle low) to get closer to it and use jk once there.

Or if the line has empty lines above / below it you could type the super handy { or } to help you jump close.

Or you could use the mouse if you're on a laptop where the trackpad is nearby + in gvim . I'm serious about that, even though I'd probably never do it. if you've played enough first person shooters your trigger finger may be more accurate and fast than any keyboard shortcut ;)

Despite all of Vim'm crazy keyboard power, there is not always a direct nor good way to do something, like very specific jumps. You need to keep and continuously build a bag of tricks to help you move around and use the appropriate ones for the job. I use all of the above fairly evenly and often (I use relativenumber), depending on what I think would be fastest. EasyMotion is pretty handy except that 5 keystrokes to perform one command is really awkward and error prone to type.

So in my humble few years of Vim experience, I'd say there is no good way to do it, but there are a lot of mediocre ways, that you can combine into a decent way.

like image 174
Andy Ray Avatar answered Nov 15 '22 10:11

Andy Ray