0

Okay, here's the thing: let's say you have a buffer, 1000 lines long (a source code for example). And you want a super-fast navigation, jumping to the right place. Example:

You are on a line 42, on character 44 (quotation after exclamation mark). You would like to access line 20, character 34 (w of which_has):

20    def very_interesting_function(which_has, some_interesting, arguments):
...
42         s = "I want to master the universe!"

What's the quickest way to get there? Plugins, custom keybinds are welcome!

My solutions:

  1. ?whichi
  2. 22k%a -- requires simple math
  3. 20ggf(a
  4. hold k and then f(a -- takes too long
  5. hold { or (, then j or k if needed, f(a -- too lame

But this is just an easy example. The use case may be far more complicated -- getting to a position of the screen, where 2) and 3) are unfeasible.

So, what you guys use? I feel like searching (/, ?) is most superior.

Edit: looks like my question may be a dupe of this one. The main difference is that Peter provided very precise answer (I couldn't find such an excellent answer in the former question).

4
  • 1
    This seems very opinion-based ... You're not really asking anything, except "which of these 3 solutions is considered the best" (since the last 2 are considered too "long" or "lame")... – Martin Tournoij Feb 25 '15 at 22:32
  • My point really is: is there a best practice (or a plugin) for precise vertical movement? Horizontal movement is pretty easy: f, t – Tomas Tomecek Feb 25 '15 at 22:46
  • 3
    "So, what do you guys use?" is more of a poll than a real question. – Doorknob Feb 25 '15 at 22:48
  • 1
    What do you mean by "precise vertical movement"? You're describing moving to a different column as well as a different row; that's not precisely "vertical." It sounds like you're just asking "how do I perform an arbitrary movement in Vim" without noticing that (to give an off-the-cuff estimate) 90% of Vim keystrokes are movement-related. – Kyle Strand Feb 26 '15 at 0:21
8

So many options besides the ones you provided:

  • If you know the line number then use G with a count or just a range to jump to the line. e.g. 20G or :20
  • Turn on 'relativenumber' via :set relativenumber to make short jumps easier
  • Jump to the start of previous method definition via [m. ([m can take a count as well)
  • Use [[ or ]] to move to the previous or next section respectively (usually function definitions).
  • If the word is somewhere else near the cursor then do #/* and spam n till you get there. (n can take a count)
  • Use H (high), M (middle), or L (low) to jump to screen positions to shorten the distance.
  • Possibly use gd (go-to-definition) if you are on the word
  • If you use ctags then use commands such as <c-]> or :tag
  • Maybe use :cscope
  • :vimgrep/which_has/ % and use the quickfix list commands like :cn or :copen (good for very large jumps)
  • Use a plugin like easymotion or sneak.vim

Personally each situation calls for something different but it's hard to go wrong with searching with /.

For more help see:

:h G
:h :[range]
:h 'rnu'
:h [[
:h [m
:h H
:h gd
:h ctags
:h :vimgrep
:h c_%
:h quickfix
:h :cn
:h :copen
:h /
:h quickref
1
  • This is the answer I was hoping for. Thank you! – Tomas Tomecek Feb 26 '15 at 6:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.