Last active
August 29, 2015 14:13
-
-
Save svermeulen/db5772d9e26b4b11c4e5 to your computer and use it in GitHub Desktop.
Vim Completion with 'jk'
This file contains hidden or 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
let s:startedScrolling = 0 | |
function! g:TriggerCompletion() | |
if pumvisible() | |
call s:OnStartScrolling() | |
return "\<C-n>" | |
endif | |
return "" | |
endfunction | |
function! s:EnableCompletionMap() | |
inoremap <expr> jk g:TriggerCompletion() | |
inoremap <expr> Jk g:TriggerCompletion() | |
inoremap <expr> JK g:TriggerCompletion() | |
inoremap <expr> jK g:TriggerCompletion() | |
endfunction | |
augroup CustomSmartCompletion | |
autocmd! | |
autocmd InsertLeave * call <sid>ClearScrollIfStarted() | |
autocmd InsertCharPre * call <sid>OnPreEnterChar() | |
augroup END | |
function! s:OnStartScrolling() | |
iunmap jk | |
iunmap Jk | |
iunmap JK | |
iunmap jK | |
let s:startedScrolling = 1 | |
endfunction | |
function! s:OnPreEnterChar() | |
if v:char != 'j' && v:char != 'k' | |
call s:ClearScrollIfStarted() | |
endif | |
endfunction | |
function! s:OnEndScrolling() | |
call s:EnableCompletionMap() | |
let s:startedScrolling = 0 | |
endfunction | |
function! s:ClearScrollIfStarted() | |
if s:startedScrolling | |
call s:OnEndScrolling() | |
endif | |
endfunction | |
function! s:ExprClearScrollIfStarted(keys) | |
call s:ClearScrollIfStarted() | |
return a:keys | |
endfunction | |
function! s:AttemptScrollUp() | |
if pumvisible() && s:startedScrolling | |
return "\<c-p>" | |
endif | |
return 'k' | |
endfunction | |
function! s:AttemptScrollDown() | |
if pumvisible() && s:startedScrolling | |
return "\<c-n>" | |
endif | |
return 'j' | |
endfunction | |
inoremap <expr> k <sid>AttemptScrollUp() | |
inoremap <expr> j <sid>AttemptScrollDown() | |
" Stop auto-scorlling if arrow keys are used | |
inoremap <expr> <left> <sid>ExprClearScrollIfStarted("\<left>") | |
inoremap <expr> <right> <sid>ExprClearScrollIfStarted("\<right>") | |
call s:EnableCompletionMap() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment