7

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.

Justin M. Keyes
  • 6,679
  • 3
  • 33
  • 60

1 Answers1

8

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/")))

Marten
  • 1,336
  • 10
  • 16
  • Thanks a lot. I read the help for `(edmacro-mode)` but I'm still not clear on why I need to separate the characters by spaces. The help says that `"g/r"` would be considered a "word", and it seems to me that multi-character words are allowed. – Justin M. Keyes Dec 08 '13 at 17:26
  • You have to separate them, because you are using the `kbd` macro, a "precompiler". It compiles the string `"g / r"` probably to `"g/r"`, so you could replace `(kbd "g / r")` with `"g/r"`, but I, for my part, think it is much less readable, especially when you bind bindings like `"C-c g C-M-a"`, so I use `kbd` everywhere. – Marten Dec 09 '13 at 09:27