4

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>
1

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)))))
  • (define-key evil-insert-state-map (kbd "; <return>") 'move-end-of-line) disables insertion of regular semicolon as Emacs expects another key and I see ;- in the minibuffer. I guess to fix it I need to use (read-event) in the command that is mapped to ";" instead of defining a key sequence, but I'm not sure how to use correctly. – Mark T. Aug 14 '12 at 9:33
  • @MarkT. You're right... I'll have to dig into the Emacs documentation to hack up a function that does what you want. – djf Aug 14 '12 at 18:17
  • This works perfectly. I'll sign up to upvote. Thank you. – Mark T. Aug 15 '12 at 4:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.