Vim Tips Wiki
Register
mNo edit summary
mNo edit summary
Line 35: Line 35:
 
let index=index(g:LABELS,input[0])
 
let index=index(g:LABELS,input[0])
 
let num=(len(input)-1)*62+index+1
 
let num=(len(input)-1)*62+index+1
if index
+
if index!=-1
 
exe 'normal! ' . num . 'W'
 
exe 'normal! ' . num . 'W'
 
endif
 
endif

Revision as of 23:23, 21 March 2012

There are some things a mouse is just better at, such as making precise jumps around the screen. In some cases you are stuck on a terminal without mouse support. In that case, this function can help by labeling every word on the screen and prompting for an input. For example,

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

And the user will input "9" to jump to "fantasy". There are many ways to implement this, the method I have here is perhaps a bit idiosyncratic:

  • The labels goes through the numbers, lowercase letters, and then capital letters.
  • The user inputs a "CCC" to jump to the 3rd occurence of C, with the assumption that one has some vague sense of where the 2nd or 3rd occurrence of a label is.
  • This is simple but works fairly well, since supposedly one has one's eye on the desired location already, so that "fantasy" changing to "kantasy" is enough to tell me the label is "k" or perhaps "kk".
  • The function here takes one argument, which is the scope of the labeling, depending on your screen size. Right now this only goes up to 185, but that can easily be extended by increasing the size of the global LABELS variable.
  • I have here included a mapping to the letter K in normal mode, since I rarely use the man page lookup.
 "186 elements
let LABELS = ["1","2","3","4","5","6","7","8","9","0","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","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","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"]

function! JumpTo(range)
	normal! H
	for i in range(0,a:range)
		exe 'normal! Wr' . g:LABELS[i]
	endfor
	redraw
	let input=input("Jump to?")
	normal! uH
	let index=index(g:LABELS,input[0])
	let num=(len(input)-1)*62+index+1
	if index!=-1
		exe 'normal! ' . num . 'W'
	endif
endfu

nnoremap K :call JumpTo(185)<CR>