Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

idea: extends the plugin to support other similar insert mode mappings #9

Open
jdhao opened this issue Nov 12, 2021 · 1 comment
Open

Comments

@jdhao
Copy link
Collaborator

jdhao commented Nov 12, 2021

For example, if we use the following mapping:

inoremap ;; <Esc>A;

We also experience lagging when we press ; in insert mode. So we can also do the same thing. We check if the time interval between pressing the two chars is below the time threshold. If that is the case, we execute its original intended mapping, otherwise, we just insert the character literally.

Here is a crude implementation:

let g:my_map = {";;": "\<Esc>mmA;\<Esc>`ma"}

let g:char_press_time = {}

inoremap <expr> ; My_map()

augroup log_press_time
  autocmd!
  autocmd InsertCharPre * call Log_char()
augroup END

function! Log_char() abort
  if v:char == ';'
    let g:char_press_time[';'] = reltime()
  endif
endfunction

function! My_map() abort
  let idx = col('.')
  let pre_idx = idx-1
  let pre_char = getline('.')[pre_idx-1]

  if pre_char == ';'
    let interval = reltimefloat(reltime(g:char_press_time[";"])) * 1000
    echomsg "interval:" interval

    if interval < 200
      return "\b" . g:my_map[";;"]
    else
      return ";"
    endif
  else
    return ";"
  endif

endfunction
@engeljh
Copy link

engeljh commented May 4, 2022

In order to avoid the step left after exiting insert mode with ESC, I now do

 inoremap <silent> kj <C-O>:stopinsert<CR>

What's the best way to use your plugin to map kj this way rather than to ESC? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants