Skip to content

Instantly share code, notes, and snippets.

@h1mesuke
Created September 1, 2011 21:14
Show Gist options
  • Save h1mesuke/1187301 to your computer and use it in GitHub Desktop.
Save h1mesuke/1187301 to your computer and use it in GitHub Desktop.
Vim - Relative Jump
" Relative Jump
call s:multi_map('nxo', 'noremap <silent> ;q :<C-u>call <SID>relative_jump(20, 0)<CR>')
call s:multi_map('nxo', 'noremap <silent> ;a :<C-u>call <SID>relative_jump(50, 0)<CR>')
call s:multi_map('nxo', 'noremap <silent> ;z :<C-u>call <SID>relative_jump(80, 0)<CR>')
call s:multi_map('nxo', 'noremap <silent> ;w :<C-u>call <SID>relative_jump(20, 20)<CR>')
call s:multi_map('nxo', 'noremap <silent> ;s :<C-u>call <SID>relative_jump(50, 20)<CR>')
call s:multi_map('nxo', 'noremap <silent> ;x :<C-u>call <SID>relative_jump(80, 20)<CR>')
call s:multi_map('nxo', 'noremap <silent> ;e :<C-u>call <SID>relative_jump(20, 66)<CR>')
call s:multi_map('nxo', 'noremap <silent> ;d :<C-u>call <SID>relative_jump(50, 66)<CR>')
call s:multi_map('nxo', 'noremap <silent> ;c :<C-u>call <SID>relative_jump(80, 66)<CR>')
call s:multi_map('nxo', 'noremap <silent> ;r :<C-u>call <SID>relative_jump(20, 100)<CR>')
call s:multi_map('nxo', 'noremap <silent> ;f :<C-u>call <SID>relative_jump(50, 100)<CR>')
call s:multi_map('nxo', 'noremap <silent> ;v :<C-u>call <SID>relative_jump(80, 100)<CR>')
function! s:relative_jump(rel_v, rel_h)
" Jump Vertically.
let lnum = line('w0') + max([1, winheight(0) * a:rel_v / 100]) - 1
let save_scrolloff = &scrolloff
set scrolloff=0
call cursor(lnum, 1)
let &scrolloff = save_scrolloff
" Jump Horizontally.
normal! $
let [bufnum, lnum, max_col, off] = getpos('.')
let min_col = strlen(matchstr(getline(lnum), '^\s*')) + 1
let col = min_col + max([1, max_col * a:rel_h / 100]) - 1
call cursor(lnum, col)
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment