Wikia

Vim Tips Wiki

Changes: Precise Jumps Without Mouse

Edit

Back to page

(Comments)
(link to script suggested in comment)
 
Line 53: Line 53:
 
</pre>
 
</pre>
   
==Comments==
+
==Related plugins==
  +
*The {{script|id=3526|text=EasyMotion}} plugin provides exactly this functionality and more.
   
Is it work mentioning [http://www.vim.org/scripts/script.php?script_id=3526 EasyMotion] here? It basically provides this functionality plus more...
+
==Comments==

Latest revision as of 17:43, November 15, 2012

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created March 21, 2012 · complexity basic · author Q335r49 · version 7.0

Making precise jumps around the screen is easy with a mouse but harder with vim's default movement commands. Perhaps your device lacks a pointer, or perhaps mousing ruins your flow. In this case we can do direct jumps by labeling the text. For example,

Is this the real life or is this just fantasy? becomes
0s 1his 2he 3eal 4ife 5r 6s 7his 8ust 9antasy?

and typing "9" would jump to "fantasy".

In the following:

  • The LABELS list can be modified, but labels are limited to a single character.
  • The labeling repeats. To remedy this there are two prompts: the first for the label, the second for which cycle (1,2,3,etc.). The screen is redrawn with the cycle number before the second prompt. So, for example, "K2" jumps to the second occurrence of K.
  • A mapping to "K" (which defaults to man lookup) is included, and is set to label the first 248 words.

Place this in your vimrc:

let LABEL = ["a","b","c",
\"d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s",
\"t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I",
\"J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y",
\"Z","1","2","3","4","5","6","7","8","9","0"]
function! GoTo(range)
    normal! Hmt
    for i in range(0,a:range)
        exe 'normal! Wr' . g:LABEL[i%len(g:LABEL)]
    endfor
    normal! 'tzt
    echo "Index?"
    redraw
    let label=nr2char(getchar())
    normal! u'tzt
    for i in range(0,a:range)
        exe 'normal! Wr' . (1+i/len(g:LABEL))
    endfor
    normal! 'tzt
    echo "Number?"
    redraw
    let offset=getchar()
    let offset=(49 <= offset && offset <= 57) ? offset-48 : 1
    normal! u'tzt
    let index=index(g:LABEL,label)
    exe 'normal! ' . ((offset-1)*len(g:LABEL)+index+1) . 'W'
endfu
nnoremap <TAB> :call GoTo(248)<CR>

Related pluginsEdit

  • The EasyMotion plugin provides exactly this functionality and more.

CommentsEdit

Around Wikia's network

Random Wiki