Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Key map for ex command in emacs evil-mode

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.

like image 524
Justin M. Keyes Avatar asked Dec 07 '13 07:12

Justin M. Keyes


1 Answers

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

like image 196
Marten Avatar answered Sep 29 '22 21:09

Marten