vimpulse: imap "jk" to ESC
Tim Harper
timcharper at gmail.com
Fri Feb 11 23:48:09 CET 2011
On Feb 11, 2011, at 15:06 , Linus Arver wrote:
> On Fri, Feb 11, 2011 at 10:51:43AM -0700, Tim Harper wrote:
>> On Fri, Feb 11, 2011 at 10:06 AM, Tim Harper <timcharper at gmail.com> wrote:
>>> On Fri, Feb 11, 2011 at 10:01 AM, Tim Harper <timcharper at gmail.com> wrote:
>>>> On Thu, Feb 10, 2011 at 11:28 PM, Linus Arver <linusarver at gmail.com> wrote:
>>>>> Hello, I'd like to know how to map "jk" to act as the ESC key in insert mode.
>>>>>
>>>>> I have tried
>>>>>
>>>>> (vimpulse-imap "jk" 'viper-intercept-ESC-key)
>>>>>
>>>>> without success.
>>>>>
>>>>> -Linus
>>>>
>>>> Try this:
>>>>
>>>>
>>>> (define-key viper-insert-basic-map (kbd "j k") 'viper-mode)
>>>>
>>>
>>> Nevermind... that's worse :) It causes j to become a prefix key,
>>> losing self-insert binding for j + any other key.
>>>
>>> It seems like a specialized function will be needed that will capture
>>> the next key and need to insert a j + resend the other key pressed to
>>> the unread-command-events... I don't know if such a facility exists in
>>> vimpulse, but would be easy enough to concoct... I'll post an example
>>> in a bit
>>>
>>
>> This works:
>>
>> https://gist.github.com/822738
>>
>> Tim
>>
>> _______________________________________________
>> implementations-list mailing list
>> implementations-list at lists.ourproject.org
>> https://lists.ourproject.org/cgi-bin/mailman/listinfo/implementations-list
>
> Excellent, it works! Thanks so much Tim.
>
> I'll repost the code here for anyone else who is interested in this kind
> of thing (a common scenario among Vim users is to map "jj" to the ESC
> key).
>
> ; make "jk" behave as ESC key
> (defun viper-escape-if-next-char (c)
> "Watches the next letter. If c, then switch to viper mode, otherwise insert a j and forward unpressed key to unread-command-events"
> (self-insert-command 1)
> (let ((next-key (read-event)))
> (if (= c next-key)
> (progn
> (delete-backward-char 1)
> (viper-mode))
> (setq unread-command-events (list next-key)))))
>
> (defun viper-escape-if-next-char-is-k (arg)
> (interactive "p")
> (if (= arg 1)
> (viper-escape-if-next-char ?k)
> (self-insert-command arg)))
>
> (define-key viper-insert-basic-map (kbd "j") 'viper-escape-if-next-char-is-k)
>
> -Linus
I prefer the gist, since it allows me to make updates to the code at a semi-authoritative source rather than burdening the users of hunting through a series of emails to find the optimal version of the code, but fine :)
TIm
More information about the implementations-list
mailing list