Reputation: 52977
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"?
Upvotes: 7
Views: 1088
Reputation: 32066
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/e
Enter (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.
Upvotes: 7
Reputation: 3669
One way of jumping to char on the same line was not mentioned, yet. Use
61|
61"pipe", to jump directly to [b]ark in control mode.
I didn't incorporate this into my regular jumping, because the number is usually a guess. with with fixed 80 chars per line you can get used to it and it will be very efficient. But the pipe key is inconvenient imo. I'm planning on using it more often because of its elegance and efficieny.
Upvotes: 0
Reputation: 18208
Kev's answer works assuming bark
is an uncommon word. You could be hitting n quite a few times.
I'd go with
671G to jump to the line,
$ to jump to the end of the line, and
2b to drop the cursor on the b.
Edit: reading over Andy's answer, I really have to agree with this:
Despite all of VIM's crazy keyboard power, there is not always a direct nor good way to do something, like very specific jumps.
I would opt for navigating around in approximate steps, like 9j to go down 9 lines. When you do this on reflex, you can navigate much faster than it takes to read line numbers or character position. It might end up being more keystrokes, but I prefer the extra speed.
Upvotes: 1
Reputation: 3649
Turn on relative line numbers and the 31j
becomes a VERY natural motion; this gets you to the line you need to be on.
Then, look for a unique character to the line near where you want to be. In this case .
is unique and just before bark. So, f.
. Then l
and you're good.
So in total: 31jf.l
.
(This is not always how it will work. Sometimes it's just as quick to use /bark
once you've jumped to the line. Sometimes it's quicker to use /bark
initially and just n
a number of times to the line. Vim give you the tools, your code will always differ.)
Upvotes: 1
Reputation: 1865
Actually, I don't want to say that I think the most efficient way is use your mouse,
another way use command line: /.*dog\&.*bark
this will reduce the frequency compared to /bark(and many n maybe)
Upvotes: 0
Reputation: 29401
If you wanted to increase the likelihood of a hit (ie. reducing the likelihood of having to hit n) using @Kev's suggestion, perhaps change your regular expression to include more text and offset your cursor from there. eg:
/dog\.bark/e-3
Upvotes: 1
Reputation: 161674
Press /barkEnter. (maybe n is required)
(It depends on the shape of text. Without the information, I cannot provide a best way.)
Upvotes: 6