Vim Tips Wiki
(Insert TipProposed template + minor manual clean)
(link to script suggested in comment)
 
(4 intermediate revisions by 3 users not shown)
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
Making precise jumps around the screen is easy with a mouse but hard with Vim's default search and movement schemes. 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,
+
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,
:<tt>Is this the real life or is this just fantasy?</tt> becomes
+
:<code>Is this the real life or is this just fantasy?</code> becomes
:<tt>0s 1his 2he 3eal 4ife 5r 6s 7his 8ust 9antasy?</tt>
+
:<code>0s 1his 2he 3eal 4ife 5r 6s 7his 8ust 9antasy?</code>
 
and typing "9" would jump to "fantasy".
 
and typing "9" would jump to "fantasy".
   
 
In the following:
 
In the following:
 
*The LABELS list can be modified, but labels are limited to a single character.
 
*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.
*The labeling repeats, jump to the 3rd occurrence of C by typing "CCC". This works fairly well in practice but a longer LABELS list helps.
 
*A mapping to "K" (which defaults to man lookup) is included, and is set to label the first 185 words.
+
*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]]:
 
Place this in your [[vimrc]]:
 
<pre>
 
<pre>
let LABELS = ["1","2","3","4","5","6","7","8","9","0","a","b","c",
+
let LABEL = ["a","b","c",
 
\"d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s",
 
\"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",
 
\"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",
 
\"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"]
\"Z"]
 
 
function! GoTo(range)
 
 
normal! Hmt
function! JumpTo(range)
 
 
for i in range(0,a:range)
normal! Hmt
 
 
exe 'normal! Wr' . g:LABEL[i%len(g:LABEL)]
for i in range(0,a:range)
 
 
endfor
exe 'normal! Wr' . g:LABELS[i%len(g:LABELS)]
 
 
normal! 'tzt
endfor
 
  +
echo "Index?"
"zt scrolls view so cursor is at first screen line
 
 
redraw
normal! 'tzt
 
  +
let label=nr2char(getchar())
redraw
 
 
normal! u'tzt
let input=input("Jump to? ")
 
  +
for i in range(0,a:range)
normal! u'tzt
 
 
exe 'normal! Wr' . (1+i/len(g:LABEL))
let index=index(g:LABELS,input[0])
 
  +
endfor
let num=(len(input)-1)*len(g:LABELS)+index+1
 
if index!=-1
+
normal! 'tzt
  +
echo "Number?"
exe 'normal! ' . num . 'W'
 
  +
redraw
endif
 
  +
let offset=getchar()
endfunction
 
  +
let offset=(49 <= offset && offset <= 57) ? offset-48 : 1
nnoremap K :call JumpTo(185)<CR>
 
  +
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>
 
</pre>
 
</pre>
  +
  +
==Related plugins==
  +
*The {{script|id=3526|text=EasyMotion}} plugin provides exactly this functionality and more.
   
 
==Comments==
 
==Comments==

Latest revision as of 17:43, 15 November 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 plugins[]

  • The EasyMotion plugin provides exactly this functionality and more.

Comments[]