Key map for ex command in emacs evil-mode

In emacs evil-mode, how do I bind a sequence of keys so that it pre-populates the ex-mode command line and positions the cursor? In vim, I can do this:

nnoremap g/r :%s//g<left><left> 

In emacs, I tried this (and several options):

 (define-key evil-normal-state-map "g/" nil) (define-key evil-normal-state-map (kbd "g/r") (lambda () (interactive) (kbd ":%s/"))) 

This has no effect, and I do not see any messages after trying to layout the keyboard.

It seems emacs used the useful evil-ex-read-command function, which sent the input to the evil mode command line:

https://github.com/magnars/.emacs.d/blob/master/site-lisp/evil/evil-ex.el#L554

But this feature is no longer available.

+7
emacs evil-mode
source share
1 answer

If you want to bind a keyboard shortcut

  • Press and release g
  • Press and release /
  • Press and release the r button

Your line in kdb should be "g / r" .

Emacs is not based on keystrokes, since there is vim, but keystrokes are just a means to perform functions. Therefore, pressing k in normal mode does not execute the function k (as in vim), but self-insert-char . This means that you are not binding the g / r combination to other keys, but rather to calling an arbitrary function. And evil defines the evil-ex function, which does exactly what you want (in fact, it is the exact function that is called when you press : in normal mode).

Unconfirmed, but it should work

(define-key evil-normal-state-map (kbd "g / r") (lambda () (evil-ex "%s/")))

+6
source share

All Articles