0

I would like to write a script, maybe even using Python, that captures a key stroke in a Vim window. Let's say I am in insert mode and I type the letter g. I would like this key stroke to trigger some action, that I configure, while still typing in the letter. Also, this behaviour should not depend on the key that has been hit (in that case a simple remapping would do). So, I guess what I'm looking for is some kind of key-hit event I can exploit. Is such a thing available in Vimscript (possibly integrated with Python)?

  • What's the status? Does the answer below address your questions? – B Layer Mar 13 at 8:12
2

There is an auto-command event InsertCharPre that detects entry of any character while in Insert mode yet still inserts it into the buffer. Here's a dead simple example (put this in your vimrc, for instance):

augroup InterceptKeyPress
    autocmd!
    autocmd InsertCharPre * :echo localtime()
augroup END

You can run any vimscript you want in response to the event.

A quick test confirms it works as expected. The impact on overall usability is something you'll have to evaluate for yourself. :)

|improve this answer|||||
  • 1
    Ah, the flickering!!! – D. Ben Knoble Feb 13 at 5:02
  • Hehe, yeah, that's one of the things I had in mind. Gotta admit, though, I've been playing with it and it runs pretty smoothly for me...but I'm running kind of a beast of a PC so there's that. – B Layer Feb 13 at 5:06
  • Ah, only flickering in the messages-line :P – D. Ben Knoble Feb 13 at 5:16
  • 1
    Yeah, with showmode set theres flickering between “INSERT” and “hello” – D. Ben Knoble Feb 13 at 5:17
  • I have vim-airline going and it's still like butter. :) – B Layer Feb 13 at 5:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.