Question

In my ~/.vimrc I have had the following two keybindings to make getting into command mode easier for myself over the past fifteen years:

nmap ; :
vmap ; :

Is there an easy way to do this in emacs evil mode?

I have been reading through the wiki at http://www.emacswiki.org/emacs/Evil and haven't come across the correct way as of yet...

Was it helpful?

Solution

Binding a key to a command

The command that gets executed when you hit : is evil-ex, as you can find out by running:

:describe-key<CR>
:

(i.e. run Emacs' describe-key command, then hit the key you want info on).

So you can just bind ; to also run evil-ex.

(define-key evil-normal-state-map (kbd ";") 'evil-ex)
(define-key evil-visual-state-map (kbd ";") 'evil-ex)
(define-key evil-motion-state-map (kbd ";") 'evil-ex)

Binding one key sequence to another

In Emacs, just generally, you can define keyboard macros, which are basically the same as how Vim handles mappings:

(global-set-key (kbd ";") (kbd ":"))

In evil-mode, you need to add them to the relevant keymap for the state:

(define-key evil-normal-state-map (kbd ";") (kbd ":"))
(define-key evil-visual-state-map (kbd ";") (kbd ":"))
(define-key evil-motion-state-map (kbd ";") (kbd ":"))

However, for some reason that does bizarre things in this case. It's the first binding I've seen fail in that way. Just use the first version.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top