blue123
blue123

Reputation: 3127

How to quickly navigate to a certain position using VIM

Below is a code snippet that I get from the internet as an example. Say we are at line 48 (the secong input near the bottom), however, we need to go back to the second of at line 8.Do we just hit :82o?

What if the line number is really large (e.g., 1425) and there are a few of the same word within the line.

In this case, it will take many key strokes, even I have to hit w after I get to the line. How can VIM compete with a mouse in a scenario of random access of a certain word in a large file, in which case you just need to move your pointer with the mouse to achieve this?

Edit: The code is a snippet that I got from the internet in order to demostrate my question. The possible scenario could be I am working on line 1148 and I'd like to go back to line 1108 just to change a word in a comment or I need to fix a bug by making a little bit change within the line 1108. Therefore, the place to go back is hardly predictable and pretty much random.

Edit: I would prefer to know a best practice to achieve this with a vanilla Vim. But you can feel free to provide a solution based on a plugin.

<HTML>
<HEAD><TITLE> Banner</TITLE>

<SCRIPT LANGUAGE= "javascript">
// Puts the text to scroll into variable called sent - SECTION A
// uses length propert to assess its length and put into variable slen
// initalizes a,b,n, and subsent variables
var sent = "This is a demonstration of a banner moving from the left to right. It makes use of the substring property of Javascript to make an interesting display"
var slen = sent.length
var siz = 25
var a = -3, b = 0
var subsent = "x"

// Creates a function to capture substrings of sent - SECTION B
function makeSub(a,b) {
subsent = sent.substring(a,b) ;
return subsent;
}

//Creates a function that increments the indexes of the substring - SECTION C 
//each time and calls the makeSub() function to geneate strings
//a indicates start of substring and siz indicates size of string required
function newMake() {
a = a + 3;
b = a + siz
makeSub(a,b);
return subsent
}

//function uses loop to get changing substrings of target - SECTION D
//repeatedly calls newMake to get next substring
//uses setTimeout() command to arrange for substrings to display 
// at specified times
function doIt() {
for (var i = 1; i <= slen ; i++) {
setTimeout("document.z.textdisplay.value = newMake()", i*300);
setTimeout("window.status = newMake()", i*300);
}
}

</SCRIPT> 
</HEAD>

<BODY >
<HR> <CENTER>
<FORM NAME="z">
<INPUT NAME="textdisplay" TYPE="text" SIZE=25> <P>
<INPUT NAME="doit" Type="button" value = "Run Banner" onClick = "doIt()"> 
</FORM></CENTER>

<HR>

</BODY></HTML>

Upvotes: 1

Views: 868

Answers (3)

Bryan Bugyi
Bryan Bugyi

Reputation: 149

Keyword Search

For random access, keyword search using \{keyword} is normally your best option. Although I have a feeling that this question (with its use of the word of) was geared to sabotage this method, I will point out that you could still run this search by just continuing to type the next word after of. With this approach we are looking at 5-7 keystrokes at most I'd say. That's quite a lot better than using the mouse if you ask me.

EasyMotion

Several others have mentioned the EasyMotion project. I am not too familiar with this project so I will not comment on it any further.

Why to really avoid the mouse?

I do not believe that the added efficiency one acquires by using Vim is the result of avoiding the mouse at all costs; rather, it is the result of training yourself to avoid leaving the home row at all costs. It is not the mechanics of the mouse but the constant task-switching between keyboard to mouse that slows us down the most.

Upvotes: 0

romainl
romainl

Reputation: 196916

Editing is not something you do at random like in your example. Navigation usually happens from one point of interest to another related point of interest, whether both POIs are part of the same task or parts of related tasks.

Your first example is very unrealistic for two reasons:

  1. because there would be no reason whatsoever to jump from that first POI to that second POI as they are completely unrelated,
  2. because the whole file may not fit wholly in the editing window, which would make the mouse largely irrelevant.

The only potentially useful navigation from your initial state would be to jump to the definition of the event handler, which is only a ?do<CR> away. Note that the function is already under our nose so there's no need to navigate to begin with if all we want is to know what doIt() does.

Your second example is too abstract to work with.

The local navigation tools at your disposal correspond to various zoom levels:

  • gegEbBwWeE at the word level,
  • fFtT;,0^$g_g^g$ at the line level,
  • (){} at the paragraph level,
  • ggnG at the buffer level,
  • nHMnL at the window level,
  • and a bunch of other commands at various levels like hjkl or gm.

But the most powerful of the lot is search, ?/nN (together with set incsearch), which lets you jump directly to wherever you want with minimal effort.

Upvotes: 1

sudo bangbang
sudo bangbang

Reputation: 28229

Best way to navigate if you know which word you should end up at is to search for it.

I'd do a /of to search for of and I'm there.

I find this easier than moving my mouse pointer.

Now coming to the scenario of really large files, relativenumbers can help you.

set relativenumber

Now to go from 1148 to 1104, you can just do a 40k then use f to get to desired character.

You can prefix f with numbers to move to nth appearance of that character. You can search too.

Again I find this easier than using mouse.

If you have enough command over searching and moving with jk prefixed with motion numbers, you'll be faster than using mouse.

Upvotes: 2

Related Questions