Skip to content

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

@jdhao

Description

@jdhao

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions