Vim
Vim copied to clipboard
`:vim.send-key` command not working for some keys?
I'm trying to answer the following Stack Overflow question:
key bindings - Bind multiple actions to one key in lighttable - Stack Overflow
It seems like I should be able to add the following to my user.keymap file to implement this:
[:editor "esc" :auto-complete.remove (:vim.send-key "<Esc>")]
But it doesn't work.
Trying to isolate what's going on, I added the following to my user.keymap file instead:
[:editor "capslock" (:vim.send-key "<Esc>")]
But it doesn't work. Nor does ... (:vim.send-key "Esc")
.
But other argument values, like "$" or "0" do work. What's up with Escape?
:vim.send-key
has a different effect for me - all of the keys trigger normal mode keys regardless of whether I'm in insert or normal mode. For example, [:editor "esc" (:vim.send-key "j") (:vim.send-key "j")]
, moves my cursor down 2 lines. What do you see?
Given that send-key just calls handleKey, it's probably worth trying handleKey in a non LT context and asking on the codemirror mailing list if you want handleKey to trigger insert key behavior
Closing as I haven't heard back. Feel free to reopen if you have investigated this more
The post "Changing a Vim editor to normal mode" on the CodeMirror Google Groups group is really really relevant ( :smile: ):
Matt Mower:
Given a reference to a CodeMirror instance using Vim-mode, can anyone tell me how to set it to normal mode?
The best I could come up with from looking at the code was to use
handleKey(cm,"<Esc>")
to emulate the user pressing Escape. If the editor is in normal mode sendinghandleKey(cm,"A")
does correctly put it in append-at-end-of-line-mode. But neither sending <Esc> (or C-[ or <C-c> cribbed from vim.js) seems to work although my naive reading of the code forhandleKey
suggests that it should.What I am trying to do is create a plugin for LightTable that returns any Vim editors to normal mode when the application loses focus. Using LT I am able to get a reference to the CodeMirror instance in any editor tab and send it messages.
Yunchi Luo:
There is no method in the API to do so. You could emulate a browser event on code mirror's hidden input field for the ESC key. Just google to see how to emulate a browser event.
Otherwise, you could send a patch to expose
exitInsertMode()
on vimApi so you can callCodeMirror.Vim.exitInsertMode(cm)
. It seems like a harmless enough change.Chris Granger:
@mightguava, seems like we should be able to do this through
handlekey
, any idea why we can't?Yunchi Luo:
You can't,
handleKey
only handles keys in normal mode and visual mode, not insert mode, not to mention that it's marked as a testing hook and should not be used externally.Matt Mower:
Thanks Yunchi.
To get my plugin working asap I went the event route and, modulo a lot of fiddling to figure out how to JS stuff without jQuery, that works just fine.
Please un-assign me.