From 9a7ac4c9d191117aa86808de8c056338bca1dc09 Mon Sep 17 00:00:00 2001 From: jdhao Date: Tue, 24 Nov 2020 22:55:39 +0800 Subject: [PATCH] Using jk to escape insert mode without lagging. See https://jdhao.github.io/2020/11/23/neovim_better_mapping_for_leaving_insert_mode/. --- autoload/utils.vim | 30 ++++++++++++++++++++++++++++++ core/mappings.vim | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/autoload/utils.vim b/autoload/utils.vim index c0eab5f..4bab8b9 100644 --- a/autoload/utils.vim +++ b/autoload/utils.vim @@ -145,3 +145,33 @@ function! utils#Get_titlestr() abort return l:title_str endfunction + +function! utils#EscapeInsertOrNot() abort + " If k is preceded by j, then remove j and go to normal mode. + let line_text = getline('.') + let cur_ch_idx = utils#CursorCharIdx() + let pre_char = utils#CharAtIdx(line_text, cur_ch_idx-1) + if pre_char ==# 'j' + return "\\" + else + return 'k' + endif +endfunction + +function! utils#CharAtIdx(str, idx) abort + " Get char at idx from str. Note that this is based on character index + " instead of the byte index. + return strcharpart(a:str, a:idx, 1) +endfunction + +function! utils#CursorCharIdx() abort + " See https://vi.stackexchange.com/a/28144/15292 + " A more concise way to get character index under cursor. + let cursor_byte_idx = col('.') + if cursor_byte_idx == 1 + return 0 + endif + + let pre_cursor_text = getline('.')[:col('.')-2] + return strchars(pre_cursor_text) +endfunction diff --git a/core/mappings.vim b/core/mappings.vim index abcd18c..ee51d53 100644 --- a/core/mappings.vim +++ b/core/mappings.vim @@ -9,7 +9,7 @@ xnoremap ; : nnoremap q; q: " Quicker in insert mode -inoremap jk +inoremap k utils#EscapeInsertOrNot() " Turn the word under cursor to upper case inoremap viwUea