Key map for ex command in emacs evil-mode

3k views Asked by At

In emacs evil-mode, how do I bind a key sequence so that it pre-populates the evil-mode ex 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 variations):

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

It has no effect, and I don't see any messages after trying the keymap.

It looks like emacs used to have a useful function evil-ex-read-command that sent input to the evil-mode command line:

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

But that function doesn't seem to be available anymore.

1

There are 1 answers

2
Marten On BEST ANSWER

If you mean to bind the key combination

  1. Press and release g
  2. Press and release /
  3. Press and release r

your string in kdb should be "g / r".

Emacs is not based on keystrokes as vim is, but keystrokes are just a means to execute functions. So pressing k in normal mode does not execute the function k (as in vim), but self-insert-char. That means that you do not bind the combination g / r to equal some other keystrokes, but rather to call an arbitrary function. And evil defines an evil-ex function, that does exactly what you want (Actually it's the exact function, that is called, when you press : in normal mode).

Untested but it should work

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