0

I'd like to be able to exit view mode by pressing "q". However, in evil "q" is bound to evil-record-macro, even in view mode. I wasn't able to find a solution using evil-define-key.

I was excited to find a similar question here, which proposes using eval-after-load, like so:

(defun testing ()
  (interactive)
  (message "Testing..."))

(eval-after-load "view" '(evil-define-key 'normal view-mode-map "q" #'testing))

This produces some strange behavior. When I enter view mode, C-h k q reports the following: "q runs the command evil-record-macro (found in evil-normal-state-map)... It is bound to q.". But, I discovered that if I do press "q", record a macro, and press "q" again to exit the recording, I see the "Testing..." message. Following this, pressing "q" continues to produce the message, and I no longer enter macro recording. "C-h k q" reports: q runs the command testing... It is bound to q, <normal-state> q.

Can someone propose a solution or workaround? Thanks!

  • By using the evil-collection package, you can selectively enable sensible evil keybindings for various modes, including view-mode. – Mark Nov 13 '18 at 19:58
3

Interestingly enough, Evil does define an initial state for view-mode, but it won't do anything because view-mode is a minor mode and initial states are for major modes only. The correct way to change the associated state is by using a hook:

(add-hook 'view-mode-hook 'evil-motion-state)

That way, q does the right thing, simply because it is not bound in evil-motion-state-map and Emacs looks up the mode-specific key instead. You can alternatively enter Emacs state by adding evil-emacs-state to the hook, like I do for more vanilla behavior.

  • It worked! Thank you very much. As an aside, I discovered that the following made my leader key (SPC) work in the *Messages* buffer, where previously it did not. (switch-to-buffer "*Messages*") (evil-motion-state) – the-konapie Apr 13 '17 at 15:04

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.