Defining key sequences in Evil-mode Emacs

elispemacskey mappingkeyboard shortcutsvi

I couldn't find any instructions on defining key sequences in the Evil doc.

The example given by the developers only covers a single key.

(define-key evil-normal-state "w" 'foo)

What do I need to do, if I want to define "gv" in the normal mode or ";RET" in the insert mode?

In Vim, for example, I just do:

imap ;<cr> <end>;<cr>
map gv :tabprev<cr>

Best Answer

The Evil wiki page suggests using elscreen.el to emulate Vim's tabs:

(load "elscreen" "ElScreen" t)

(define-key evil-normal-state-map (kbd "C-w t") 'elscreen-create) ;creat tab
(define-key evil-normal-state-map (kbd "C-w x") 'elscreen-kill) ;kill tab

(define-key evil-normal-state-map "gv" 'elscreen-previous) ;previous tab
(define-key evil-normal-state-map "gt" 'elscreen-next) ;next tab

Similarly you could define:

(define-key evil-insert-state-map (kbd "; <return>") 'move-end-of-line)

This emulates imap ;<cr> <end>;<cr>. If you press ; followed by Return then the cursor will jump to the end of the line and insert a semicolon. I would have liked to make it a little more generic, but I'm missing a key function.

(define-key evil-insert-state-map (kbd ";") 'insert-or-append)

(defun insert-or-append ()
  "If the user enters <return>, then jump to end of line and append a semicolon,
   otherwise insert user input at the position of the cursor"
  (interactive)
  (let ((char-read (read-char-exclusive))
        (trigger ";"))
    (if (eql ?\r char-read)
        (progn
          (end-of-line)
          (insert trigger))
      (insert (this-command-keys)))))
Related Question