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 LayerCommented Mar 13, 2020 at 8:12
Add a comment
|
1 Answer
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. :)
-
1
-
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 LayerCommented Feb 13, 2020 at 5:06
-
-
1Yeah, with showmode set theres flickering between “INSERT” and “hello”– D. Ben Knoble ♦Commented Feb 13, 2020 at 5:17
-