Created
February 21, 2014 09:09
-
-
Save tana/9131084 to your computer and use it in GitHub Desktop.
Vimで二分探索っぽくカーソルを移動する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 二分探索のようにカーソルを移動する | |
" | |
" 操作方法 | |
" 現在位置より左に行きたい場合は <C-H> | |
" 右に行きたい場合は <C-L> (再描画を上書きするが、大丈夫だろう) | |
" <C-K> で状態をリセット | |
" 行を移動するとリセットされる。 | |
let s:left = 0 " 範囲の左端 | |
let s:right = 0 " 範囲の右端 | |
function! OnCursorMoved() | |
let l:now_line = line(".") | |
if !exists("s:old_line") | |
let s:old_line = -1 | |
endif | |
if s:old_line == l:now_line | |
return | |
endif | |
let s:old_line = l:now_line | |
" 行が移動したら実行される | |
call Reset() | |
endfunction | |
function! GoLeft() | |
let s:right = col(".") | |
call cursor(line("."), (s:left + s:right) / 2) | |
endfunction | |
function! GoRight() | |
let s:left = col(".") | |
call cursor(line("."), (s:left + s:right) / 2) | |
endfunction | |
function! Reset() | |
let s:left = 0 | |
let s:right = strlen(getline(".")) | |
endfunction | |
nnoremap <silent> <C-H> :call GoLeft()<CR> | |
nnoremap <silent> <C-L> :call GoRight()<CR> | |
nnoremap <silent> <C-K> :call Reset()<CR> | |
" 二重にAutoCmdがセットされることを防ぐ | |
if !exists("s:autocmd_is_set") | |
autocmd CursorMoved * call OnCursorMoved() | |
let s:autocmd_is_set = 1 | |
endif | |
call OnCursorMoved() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment